pytwine.parsers.Parser

class pytwine.parsers.Parser(file: Optional[TextIO] = None, string: Optional[str] = None)[source]

Bases: object

Reads and parses twineable documents into a list of Chunks.

Has a concept of a code block marker, i.e. something like markdown’s backtick fenced code blocks:

```

or tilde fenced code blocks:

~~~

Code blocks are considered to start on the line where the code block marker begins, and end on the line of their end marker.

Empty blocks, and code blocks containing only whitespace, are skipped.

Code blocks can have pandoc-style options. e.g. for a Markdown document, the options would be given like so:

```python .important startLine=101 animal="spotted lynx"

where dot gives a class and the = gives attributes.

The Parser doesn’t parse or process options at all, but just stores the whole unparsed start-of-block line in the block_start_line attribute of the CodeChunk; it’s up to Processor classes to parse and potentially make use of the options.

Only subclass so far is MarkdownParser.

Subclasses should override _is_codeblock_start and _is_codeblock_end (see the code for details).

sample usage:

sample usage using MarkdownParser subclass:

>>> parser = MarkdownParser(string="some stuff")
>>> chunks = parser.parse()
>>> chunks
[DocChunk(chunkType='doc', contents='some stuff', number=1, startLineNum=1)]
__init__(file: Optional[TextIO] = None, string: Optional[str] = None)[source]
Keyword Arguments
  • file – path to a file to be processed

  • string – a string to be processed

One of either file or string must be given.

Methods

__init__([file, string])

keyword file

path to a file to be processed

parse()

Parse the source and return a list of Chunks.

parse() List[pytwine.core.Chunk][source]

Parse the source and return a list of Chunks.