diff --git a/Makefile b/Makefile index 6af4048..2ae67c2 100644 --- a/Makefile +++ b/Makefile @@ -31,4 +31,4 @@ tag: git push --follow-tags echo "Update the AUR package once done!" -release: format status tag \ No newline at end of file +release: format test tag diff --git a/tests/input/latex_magic_comments.tex b/tests/input/latex_magic_comments.tex new file mode 100644 index 0000000..c5aafad --- /dev/null +++ b/tests/input/latex_magic_comments.tex @@ -0,0 +1,14 @@ +% !TEX program = lualatex +% !TEX encoding = utf-8 +% !TEX spellcheck = en-US +% !BIB program = biber + +% ################################################################ Preamble + +\documentclass[a4paper,12pt]{article} + +% ################################################################ Body + +\begin{document} + Hey student! +\end{document} diff --git a/tests/reference/latex_magic_comments.tex b/tests/reference/latex_magic_comments.tex new file mode 100644 index 0000000..977608d --- /dev/null +++ b/tests/reference/latex_magic_comments.tex @@ -0,0 +1,23 @@ +% !TEX program = lualatex +% !TEX encoding = utf-8 +% !TEX spellcheck = en-US +% !BIB program = biber + +% ┌───────────────────────────────────────────────────────────────┐ +% │ Contents of latex_magic_comments.tex │ +% ├───────────────────────────────────────────────────────────────┘ +% │ +% ├── Preamble +% ├── Body +% │ +% └─────────────────────────────────────────────────────────────── + +% ################################################################ Preamble + +\documentclass[a4paper,12pt]{article} + +% ################################################################ Body + +\begin{document} + Hey student! +\end{document} diff --git a/toc/toc.py b/toc/toc.py index c4594f7..591699b 100644 --- a/toc/toc.py +++ b/toc/toc.py @@ -288,7 +288,8 @@ def _add_toc(self, outerToc: str) -> None: def _check_directives(self, outerToc: str) -> str: # if a frontmatter, shebang or directive is found, append after first line(s) _data = self._read_file() - _firstLine = _data.splitlines()[0] + _lines = _data.splitlines() + _firstLine = _lines[0] if _firstLine == "": # if _firstLine was be empty, re.sub would destroy the original file by inserting an outerToc between every character _data = outerToc + "\n" + _data @@ -350,6 +351,19 @@ def _check_directives(self, outerToc: str) -> str: _firstFewLines = outerToc + "\n\n" + _firstLine else: _firstFewLines = outerToc + "\n\n" + _firstLine + case "bib" | "cls" | "erl" | "hrl" | "mat" | "sty" | "tex": + _firstline_magic_comment = re.search(r"^% \!", _firstLine) + if _firstline_magic_comment is not None: + _magic_comment_lines = [] + for _current_line in _data.splitlines(): + if _current_line.startswith("% !"): + _magic_comment_lines.append(_current_line) + else: + break + _firstLine = "\n".join(_magic_comment_lines) + _firstFewLines = _firstLine + "\n\n" + outerToc + else: + _firstFewLines = outerToc + "\n\n" + _firstLine case _: # single line shebang, xml, html, vim, emacs, perl pod if (