diff --git a/src/bootstraparse/modules/context_mngr.py b/src/bootstraparse/modules/context_mngr.py
index c37760d..f4eec9e 100644
--- a/src/bootstraparse/modules/context_mngr.py
+++ b/src/bootstraparse/modules/context_mngr.py
@@ -5,6 +5,9 @@
# self.optionals: accessible via __rshift__ (remapped to return self.map[other]())
# and self.map: accessible via __invert__ (remapped to return self.optionals)
import rich
+from bootstraparse.modules import syntax
+from bootstraparse.modules.error_mngr import CannotBeContainedError
+from bootstraparse.modules.syntax import EncapsulatedSemanticType
class BaseContainer:
@@ -198,7 +201,49 @@ class TableCellContainer(BaseContainer):
class ContextManager:
- pass
+ """
+ Class in charge of piling all the parsed elements and then encapsulating them inside one another.
+ """
+ def __init__(self, parsed_list):
+ """
+ Takes a list of parsed tokens.
+ Parameters
+ ----------
+ parsed_list : list[syntax.SemanticType]
+ List of parsed tokens output by our parser.
+ """
+ self.output = []
+ self.parsed_list = parsed_list
+
+ def __call__(self):
+ """
+ Interprets the list of tokens provided and deduces context, encapsulating them to their closest neighbour and
+ containing all tokens inbetween.
+ Returns
+ -------
+ list[BaseContainer]
+ Returns the pile entirely processed as a list of containers.
+ """
+ pile = []
+ matched_elements = {}
+ line_number = 0
+ for token in self.parsed_list:
+ try:
+ if token.counterpart():
+ if token.counterpart() in matched_elements:
+ self.encapsulate(pile[matched_elements[token.counterpart()]:])
+ elif isinstance(token, EncapsulatedSemanticType):
+ raise CannotBeContainedError
+ else:
+ pile += token
+ matched_elements += token
+ else:
+ pile += token
+
+ except CannotBeContainedError as e:
+ print(e)
+
+
# Defines all methods to manage the context of the parser as a file is being parsed
# Builds all containers as the parser is parsing the file
@@ -206,6 +251,15 @@ class ContextManager:
if __name__ == "__main__":
from bootstraparse.modules import parser
from io import StringIO
- io_string = StringIO('test')
+ io_string = StringIO(
+ """<
>
+ # header 1 #
+ ! display 1 !"""
+ )
test = parser.parse_line(io_string)
+ ctx = ContextManager(test)
+ ctx()
# rich.print(test)
diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py
index 98ed038..7b8fee8 100644
--- a/src/bootstraparse/modules/error_mngr.py
+++ b/src/bootstraparse/modules/error_mngr.py
@@ -129,3 +129,12 @@ class ParsingError(Exception):
return "[{}] Line {}:{} {}".format(self.file, self.line, self.column, self.args[0])
else:
return str(self.message)
+
+
+class CannotBeContainedError(Exception):
+ """
+ The token is not final and cannot be contained. Indicative of a mismatched token.
+ """
+ def __init__(self, token):
+ self.token = token
+ super().__init__(f"Could not process {token.label}")
diff --git a/src/bootstraparse/modules/syntax.py b/src/bootstraparse/modules/syntax.py
index 888b10c..ce02861 100644
--- a/src/bootstraparse/modules/syntax.py
+++ b/src/bootstraparse/modules/syntax.py
@@ -5,6 +5,7 @@
import os
from itertools import zip_longest
from collections import namedtuple
+from bootstraparse.modules.error_mngr import CannotBeContainedError
import pyparsing as pp
# import rich
@@ -61,16 +62,59 @@ class SemanticType:
def __ne__(self, other):
return not self.__eq__(other)
+ def counterpart(self): # noqa
+ return None
+
+ def to_container(self):
+ """
+ Function called by the context manager to know how to deal with the encapsulation of the token.
+ Returns
+ -------
+ SemanticType
+ Self if containable, the expected replacement token if it can, raises an error otherwise.
+ Raises
+ ------
+ CannotBeContainedError
+ Raises error if the token we were trying to encapsulate cannot be (probably mismatched).
+ """
+ raise CannotBeContainedError(self)
+
class ExplicitSemanticType(SemanticType):
"""
Explicit semantic type, the label is the only information we need.
"""
+ def counterpart(self):
+ return self.label
+
def to_markup(self):
return f'<{self.label} />'
+ def to_container(self):
+ return TextToken(self.content[0])
-class EmptySemanticType(SemanticType):
+
+class FinalSemanticType(SemanticType):
+ """
+ Semantic type used by the context manager to ascertain token is possible to encapsulate.
+ """
+
+ def counterpart(self): # noqa
+ return None
+
+ def to_container(self):
+ return self
+
+
+class EncapsulatedSemanticType(SemanticType):
+ """
+ Semantic type used to signify it has a matching start component and needs to be matched.
+ """
+ # def counterpart(self):
+ # return self.label[:-4]+'start'
+
+
+class EmptySemanticType(FinalSemanticType):
"""
Empty semantic type, the content is the only information we need.
"""
@@ -96,54 +140,80 @@ class ImageToken(SemanticType):
class TextToken(EmptySemanticType):
label = "text"
+ def to_container(self):
+ return self
+
+# TODO: check tokens doc
+# TODO: all one-line elements need to inherit FinalSemanticType
class EnhancedToken(ExplicitSemanticType):
+ """Mother of all EtTokens"""
label = "text:enhanced"
class EtEmToken(ExplicitSemanticType):
+ """*"""
label = "text:em"
class EtStrongToken(ExplicitSemanticType):
+ """**"""
label = "text:strong"
class EtUnderlineToken(ExplicitSemanticType):
+ """__"""
label = "text:underline"
class EtStrikethroughToken(ExplicitSemanticType):
+ """~~"""
label = "text:strikethrough"
class EtCustomSpanToken(SemanticType):
+ """(#int)"""
label = "text:custom_span"
class EtUlistToken(SemanticType):
+ """-"""
label = "list:ulist"
class EtOlistToken(SemanticType):
+ """#."""
label = "list:olist"
class HeaderToken(SemanticType):
+ """
+ # string #
+ number of # indicates level
+ """
label = "header"
class DisplayToken(SemanticType):
+ """
+ ! string !
+ number of ! indicates level
+ """
label = "display"
class StructuralElementStartToken(SemanticType):
+ """<>"""
label = "se:end"
+ def counterpart(self): # noqa
+ return "se:start"
+
class HyperlinkToken(SemanticType):
label = "hyperlink"
@@ -200,6 +270,9 @@ class BlockQuoteAuthorToken(SemanticType):
class Linebreak(ExplicitSemanticType):
label = "linebreak"
+ def to_container(self):
+ return self
+
def of_type(token_class):
"""