Skip to content

Commit

Permalink
feat: added magic comment support for LaTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaJack committed Oct 27, 2024
1 parent c6d74e4 commit a1adfce
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ tag:
git push --follow-tags
echo "Update the AUR package once done!"

release: format status tag
release: format test tag
14 changes: 14 additions & 0 deletions tests/input/latex_magic_comments.tex
Original file line number Diff line number Diff line change
@@ -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}
23 changes: 23 additions & 0 deletions tests/reference/latex_magic_comments.tex
Original file line number Diff line number Diff line change
@@ -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}
16 changes: 15 additions & 1 deletion toc/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit a1adfce

Please sign in to comment.