Beispiel #1
0
    def __init__(self, content: PyexlatexItems, packages: PyexlatexItems = None,
                 pre_env_contents: Optional[PyexlatexItems] = None, data_cleanup_func: Optional[Callable] = None,
                 pre_output_func: Optional[Callable] = None):
        """

        :param content:
        :param packages:
        :param pre_env_contents:
        :param data_cleanup_func: should accept DocumentSetupData and modify it in place. This is called just before
            using the data.
        :param pre_output_func: function which modifies the latex string before outputting it. The function should
            accepts a single argument which is a string of the entire latex contents, and it should return a string
            which will be used as the latex contents for output.
        """
        from pyexlatex.logic.builder import build, _build
        self.init_data()
        if packages is not None:
            self.data.packages.extend(packages)

        self.add_data_from_content(content)
        self.add_data_from_content(self.document_class_obj)

        self.has_references = False
        if self.data.references:
            self.has_references = True

        if isinstance(content, (ItemBase, str)):
            content_list = [content]
        else:
            content_list = content  # type: ignore

        if pre_env_contents is None:
            pre_env_contents = []

        if data_cleanup_func:
            data_cleanup_func(self.data)

        possible_pre_env_contents = [  # type: ignore
            self.document_class_obj,
            *self.data.begin_document_items,
            *[str(package) for package in self.data.packages],
            *pre_env_contents
        ]

        self.pre_env_contents = build([item for item in possible_pre_env_contents if item is not None])

        content_list.extend(self.data.end_document_items)

        self.contents = content_list


        content_list = deepcopy(content_list)  # don't overwrite original objects
        # combine content into a single str
        content_list = build(content_list)

        if pre_output_func:
            content_list = pre_output_func(content_list)

        super().__init__(self.name, content_list, pre_env_contents=self.pre_env_contents)
Beispiel #2
0
    def __str__(self):
        from pyexlatex.logic.builder import build
        tex_obj = self.tex_obj(as_document=False)

        # TODO [#18]: restructure table module so that it works like others where items are tex generators
        #
        # Until then, need to manually call build as it won't carry through to the separate tex generator
        build(tex_obj)

        return str(tex_obj)
def test_elevate_sections_objects_success():
    content = build([SECTION])
    revised = elevate_sections_by_one_level(content)
    assert (
        revised ==
        "\\begin{chapter}{Section}\nSection content\n\\begin{section}{Sub section}\nSub content\n\\begin{subsection}{Sub Sub section}\nSub sub content\n\\end{subsection}\n\\end{section}\n\\end{chapter}"
    )
Beispiel #4
0
def format_contents(contents) -> str:
    from pyexlatex.logic.builder import _build, build

    if isinstance(contents, (list, tuple)):
        contents = [_format_content(c) for c in contents]
        contents = deepcopy(contents)
        contents = build(contents)
        return contents

    return _format_content(contents)
Beispiel #5
0
 def _write_temp_tex_file(self,
                          name: str,
                          content: PyexlatexItems,
                          directory: Path,
                          ext: str = 'tex'):
     out_path = directory / f'{name}.{ext}'
     text_content = build(content)
     out_path.write_text(text_content)
     self.data.filepaths.append(str(out_path))
     self.data.binaries.append(bytes(text_content, 'utf8'))
Beispiel #6
0
    def __str__(self):
        from pyexlatex.logic.builder import _build, build

        contents = deepcopy(self.contents)
        contents = build(contents)

        possible_items = [
            self.pre_env_contents,
            self.env.wrap(str(contents)), self.post_env_contents
        ]
        items = [item for item in possible_items if item]
        return _build(items)
def test_elevate_sections_objects_chapter():
    content = build([CHAPTER])
    with pytest.raises(ValueError) as exc_info:
        elevate_sections_by_one_level(content)
    assert 'cannot convert' in str(exc_info.value)