Beispiel #1
0
def test_documentclass_after_macro():
    # In some TeX files, the documentclass isn't declared until after some initial macros.
    # We still want to detect the documentclass in these documents.
    extractor = DocumentclassExtractor()
    tex = "\\def\year{2020}\n\\documentclass{article}"
    documentclass = extractor.parse(tex)
    assert documentclass is not None
Beispiel #2
0
def colorize_citations(
    tex: str,
    bibitems: List[SerializableEntity],
    options: ColorizeOptions = ColorizeOptions()
) -> ColorizedTex:
    """
    To save time, this function only attempts to add colorization commands to the main document file,
    as determined by the presence of the "documentclass" macro. This function will do nothing when
    applied to plain TeX (i.e. non-LaTeX) files.
    """

    documentclass_extractor = DocumentclassExtractor()
    documentclass = documentclass_extractor.parse(tex)
    if not documentclass:
        return ColorizedTex(tex, {})

    citation_color_commands_tex = ""
    citation_hues = {}
    hue_generator = generate_hues()
    for bibitem in bibitems:
        if options.preset_hue is not None:
            hue = options.preset_hue
        else:
            hue = next(hue_generator)

        citation_color_commands_tex += _get_color_citation_tex(
            bibitem.id_, hue) + "\n"
        citation_hues[bibitem.id_] = hue

    colorized_tex = add_color_macros(tex,
                                     after_macros=citation_color_commands_tex)
    return ColorizedTex(colorized_tex, citation_hues)
Beispiel #3
0
def add_color_macros(tex: str, after_macros: Optional[str] = None) -> str:
    documentclass_extractor = DocumentclassExtractor()
    documentclass = documentclass_extractor.parse(tex)
    if documentclass is not None:
        begin_document_extractor = BeginDocumentExtractor()
        begin_document = begin_document_extractor.parse(tex)
        if begin_document is not None:
            return (
                tex[: begin_document.start]
                + "\n"
                + COLOR_MACROS_BASE_MACROS
                + "\n"
                + COLOR_MACROS_LATEX_IMPORTS
                + "\n"
                + tex[begin_document.start : begin_document.end]
                + "\n"
                # These main color macros should be included *after* "\begin{document}". This is
                # because AutoTeX will sometimes add a "\RequirePackage{hyperref}" statement right
                # before "\begin{document}" in the moments before compiling the TeX. If we instead
                # put the color macros are put above "\begin{document}", what happens is that
                # hyperref reverts the hyperref macros that we had redefined to enable coloring.
                + COLOR_MACROS
                + ("\n" + after_macros if after_macros else "")
                + tex[begin_document.end :]
            )
    return (
        TEX_COLOR_MACROS + ("\n" + after_macros if after_macros else "") + "\n\n" + tex
    )
def colorize_citations(
    tex: str,
    bibitem_keys: List[str],
    batch_size: Optional[int] = None,
    preset_hue: Optional[float] = None,
) -> Iterator[CitationColorizationBatch]:
    """
    To save time, this function only attempts to add colorization commands to the main document file,
    as determined by the presence of the "documentclass" macro. This function will do nothing when
    applied to plain TeX (i.e. non-LaTeX) files.
    """

    documentclass_extractor = DocumentclassExtractor()
    documentclass = documentclass_extractor.parse(tex)
    if not documentclass:
        return

    batch_size = min(batch_size,
                     NUM_HUES) if batch_size is not None else NUM_HUES
    batch_index = 0

    while True:
        batch = bibitem_keys[batch_index * batch_size:(batch_index + 1) *
                             batch_size]
        if len(batch) == 0:
            return

        citation_color_commands_tex = ""
        colorized_citations = []
        hue_generator = generate_hues()
        for key in batch:
            if preset_hue is not None:
                hue = preset_hue
            else:
                hue = next(hue_generator)

            citation_color_commands_tex += _get_color_citation_tex(key,
                                                                   hue) + "\n"
            colorized_citations.append(ColorizedCitation(key, hue))

        colorized_tex = add_color_macros(
            tex, after_macros=citation_color_commands_tex)
        yield CitationColorizationBatch(colorized_tex, colorized_citations)
        batch_index += 1
def test_extract_documentclass_after_comment_ending_with_whitespace():
    extractor = DocumentclassExtractor()
    tex = "\n\n%\\documentclass{IEEEtran}    \n\\documentclass{article}"
    documentclass = extractor.parse(tex)
    assert documentclass is not None