quizcomp.parser.common

 1import copy
 2import typing
 3
 4ENCODING: str = 'utf-8'
 5
 6ENV_KEY_CONTEXT: str = 'qg_context'
 7
 8TOKEN_META_KEY_ROOT: str = 'qg_root'
 9TOKEN_META_KEY_STYLE: str = 'qg_style'
10
11CONTENT_NODES: typing.Set[str] = {
12    'code_block',
13    'code_inline',
14    'fence',
15    'math_block',
16    'math_inline',
17    'placeholder',
18    'text',
19    'text_special',
20}
21
22class RenderContext:
23    """ A simple struct for carrying around the context for rendering. """
24
25    def __init__(self,
26            base_dir: typing.Union[str, None] = None,
27            source_path: typing.Union[str, None] = None,
28            style: typing.Union[typing.Dict[str, typing.Any], None] = None,
29            text_allow_all_characters: bool = False,
30            text_allow_special_text: bool = False,
31            force_raw_image_src: bool = False,
32            **kwargs: typing.Any) -> None:
33        if (base_dir is None):
34            base_dir = '.'
35
36        self.base_dir: str = base_dir
37        """ A base directory to resolve any relative paths. """
38
39        self.source_path: typing.Union[str, None] = source_path
40        """ If the document to render was read from a file, this path points to that file. """
41
42        if (style is None):
43            style = {}
44
45        self.style: typing.Dict[str, typing.Any] = style
46        """ Style information to use for rendering. """
47
48        self.text_allow_all_characters: bool = text_allow_all_characters
49        """ Allow all characters when rendering as `text`. """
50
51        self.text_allow_special_text: bool = text_allow_special_text
52        """ Allow "special text" when when rendering as `text`. """
53
54        self.force_raw_image_src: bool = force_raw_image_src
55        """ Do not do any processing on an image's source. """
56
57    def copy(self) -> 'RenderContext':
58        """ Get a deep copy. """
59
60        return copy.deepcopy(self)
61
62def handle_block_style(
63        block_info: typing.Dict[str, typing.Any],
64        context: RenderContext,
65        ) -> typing.Tuple[RenderContext, typing.Dict[str, typing.Any]]:
66    """
67    Handle style associated with a block by extracting the style,
68    and creating a copy of the context with the new style (if any block style exists).
69    Returns: (context, block style).
70    """
71
72    block_style = block_info.get(TOKEN_META_KEY_STYLE, None)
73    if ((block_style is None) or (len(block_style) == 0)):
74        return context, {}
75
76    new_context = context.copy()
77    new_context.style.update(block_style)
78
79    return new_context, block_style
ENCODING: str = 'utf-8'
ENV_KEY_CONTEXT: str = 'qg_context'
TOKEN_META_KEY_ROOT: str = 'qg_root'
TOKEN_META_KEY_STYLE: str = 'qg_style'
CONTENT_NODES: Set[str] = {'math_inline', 'math_block', 'text', 'text_special', 'fence', 'placeholder', 'code_inline', 'code_block'}
class RenderContext:
23class RenderContext:
24    """ A simple struct for carrying around the context for rendering. """
25
26    def __init__(self,
27            base_dir: typing.Union[str, None] = None,
28            source_path: typing.Union[str, None] = None,
29            style: typing.Union[typing.Dict[str, typing.Any], None] = None,
30            text_allow_all_characters: bool = False,
31            text_allow_special_text: bool = False,
32            force_raw_image_src: bool = False,
33            **kwargs: typing.Any) -> None:
34        if (base_dir is None):
35            base_dir = '.'
36
37        self.base_dir: str = base_dir
38        """ A base directory to resolve any relative paths. """
39
40        self.source_path: typing.Union[str, None] = source_path
41        """ If the document to render was read from a file, this path points to that file. """
42
43        if (style is None):
44            style = {}
45
46        self.style: typing.Dict[str, typing.Any] = style
47        """ Style information to use for rendering. """
48
49        self.text_allow_all_characters: bool = text_allow_all_characters
50        """ Allow all characters when rendering as `text`. """
51
52        self.text_allow_special_text: bool = text_allow_special_text
53        """ Allow "special text" when when rendering as `text`. """
54
55        self.force_raw_image_src: bool = force_raw_image_src
56        """ Do not do any processing on an image's source. """
57
58    def copy(self) -> 'RenderContext':
59        """ Get a deep copy. """
60
61        return copy.deepcopy(self)

A simple struct for carrying around the context for rendering.

RenderContext( base_dir: Optional[str] = None, source_path: Optional[str] = None, style: Optional[Dict[str, Any]] = None, text_allow_all_characters: bool = False, text_allow_special_text: bool = False, force_raw_image_src: bool = False, **kwargs: Any)
26    def __init__(self,
27            base_dir: typing.Union[str, None] = None,
28            source_path: typing.Union[str, None] = None,
29            style: typing.Union[typing.Dict[str, typing.Any], None] = None,
30            text_allow_all_characters: bool = False,
31            text_allow_special_text: bool = False,
32            force_raw_image_src: bool = False,
33            **kwargs: typing.Any) -> None:
34        if (base_dir is None):
35            base_dir = '.'
36
37        self.base_dir: str = base_dir
38        """ A base directory to resolve any relative paths. """
39
40        self.source_path: typing.Union[str, None] = source_path
41        """ If the document to render was read from a file, this path points to that file. """
42
43        if (style is None):
44            style = {}
45
46        self.style: typing.Dict[str, typing.Any] = style
47        """ Style information to use for rendering. """
48
49        self.text_allow_all_characters: bool = text_allow_all_characters
50        """ Allow all characters when rendering as `text`. """
51
52        self.text_allow_special_text: bool = text_allow_special_text
53        """ Allow "special text" when when rendering as `text`. """
54
55        self.force_raw_image_src: bool = force_raw_image_src
56        """ Do not do any processing on an image's source. """
base_dir: str

A base directory to resolve any relative paths.

source_path: Optional[str]

If the document to render was read from a file, this path points to that file.

style: Dict[str, Any]

Style information to use for rendering.

text_allow_all_characters: bool

Allow all characters when rendering as text.

text_allow_special_text: bool

Allow "special text" when when rendering as text.

force_raw_image_src: bool

Do not do any processing on an image's source.

def copy(self) -> RenderContext:
58    def copy(self) -> 'RenderContext':
59        """ Get a deep copy. """
60
61        return copy.deepcopy(self)

Get a deep copy.

def handle_block_style( block_info: Dict[str, Any], context: RenderContext) -> Tuple[RenderContext, Dict[str, Any]]:
63def handle_block_style(
64        block_info: typing.Dict[str, typing.Any],
65        context: RenderContext,
66        ) -> typing.Tuple[RenderContext, typing.Dict[str, typing.Any]]:
67    """
68    Handle style associated with a block by extracting the style,
69    and creating a copy of the context with the new style (if any block style exists).
70    Returns: (context, block style).
71    """
72
73    block_style = block_info.get(TOKEN_META_KEY_STYLE, None)
74    if ((block_style is None) or (len(block_style) == 0)):
75        return context, {}
76
77    new_context = context.copy()
78    new_context.style.update(block_style)
79
80    return new_context, block_style

Handle style associated with a block by extracting the style, and creating a copy of the context with the new style (if any block style exists). Returns: (context, block style).