~tim-ats-d/Tex-setup

7fd7f5d1f6da87b980222c0206a2a043edde81a8 — Tim-ats-d 6 months ago 72ad7ce
Improve code quality, add a template directory.
A .gitignore => .gitignore +3 -0
@@ 0,0 1,3 @@
__pycache__
*.tex


A __pycache__/conf.cpython-37.pyc => __pycache__/conf.cpython-37.pyc +0 -0
M conf.py => conf.py +18 -57
@@ 1,70 1,31 @@

# String used for \author tag.
author: str = ""
from typing import Dict, List


# Number of spaces used to indent tag.
indent_lvl: int = 2

# Packages included at top of documents.
packages: dict[str, tuple[str]] = {
    "inputenc": "utf8",
    "multicol": None,
    "graphicx": None
}

# Document template.
article_template = r"""
\documentclass[13pt]{article}

$packages

\title{$title}
\author{$author}
\date{\today}

\begin{document}
  \maketitle

\end{document}
"""
# String used for \author tag.
author: str = ""

# Beamer theme.
beamer_theme: str = "Boadilla"

# Beamer frame template.
frame_template: str = r"""
\begin{frame}
  \frametitle{}
  \section{}

  \framesubtitle{}
  \subsection{}

\end{frame}
"""

# Presentation template.
presentation_template: str = r"""
\documentclass{beamer}

\usetheme{$theme}

$packages
# Packages included at top of documents.
# Must be a mapping object contains package name as key and options as list of string.
packages: Dict[str, List[str]] = {
    "inputenc": ["utf8"],
    "multicol": [],
    "graphicx": []
}

\title{$title}
\author{$author}

\date{\today}
class Template:
    # Document template.
    article = open("templates/article").read()

\begin{document}
  \begin{frame}
    \titlepage{}
  \end{frame}
    # Beamer frame template.
    frame = open("templates/frame").read()

  \begin{frame}
    \frametitle{Table of contents}
    \tableofcontents
  \end{frame}
$content
\end{document}
"""
    # Slideshow template.
    presentation = open("templates/presentation").read()

A templates/article => templates/article +12 -0
@@ 0,0 1,12 @@
\documentclass[13pt]{article}

$packages

\title{$title}
\author{$author}
\date{\today}

\begin{document}
  \maketitle

\end{document}

A templates/frame => templates/frame +9 -0
@@ 0,0 1,9 @@

\begin{frame}
  \frametitle{}
  \section{}

  \framesubtitle{}
  \subsection{}

\end{frame}

A templates/presentation => templates/presentation +23 -0
@@ 0,0 1,23 @@
\documentclass{beamer}

\usetheme{$theme}

$packages

\title{$title}
\author{$author}

\date{\today}

\begin{document}
  \begin{frame}
    \titlepage{}
  \end{frame}

  \begin{frame}
    \frametitle{Table of contents}
    \tableofcontents
  \end{frame}
$content
\end{document}


A test.tex => test.tex +34 -0
@@ 0,0 1,34 @@
\documentclass{beamer}

\usetheme{Boadilla}

\usepackage{{inputenc}}[utf8]
\usepackage{{multicol}}
\usepackage{{graphicx}}

\title{test}
\author{}

\date{\today}

\begin{document}
  \begin{frame}
    \titlepage{}
  \end{frame}

  \begin{frame}
    \frametitle{Table of contents}
    \tableofcontents
  \end{frame}

  \begin{frame}
    \frametitle{}
    \section{}

    \framesubtitle{}
    \subsection{}

  \end{frame}

\end{document}


M texsetup.py => texsetup.py +25 -17
@@ 1,29 1,33 @@

import argparse
from itertools import repeat
import string
import textwrap
from typing import Dict, Iterable

import conf


enclose = lambda string: "{%s}" % string
bracketed = lambda text: "{%s}" % text
hooked = lambda text: "[%s]" % text


def render_packages(packages: dict[str, tuple[str]]) -> str:
    return  "\n".join(r"\usepackage"
                      + (f"{enclose(name)}[{option}]"
                         if option
                         else enclose(name))
                      for name, option in packages.items())
def render_packages(packages: Dict[str, Iterable[str]]) -> str:
    """Return formated \usepackage tag."""
    return  "\n".join(r"\usepackage{%s}%s" % (bracketed(name),
                                              "".join(map(hooked, options)))
                      for name, options in packages.items())


def render_frames(template: str, frame_nb: int) -> str:
    return textwrap.indent("".join(template
                                       for f in range(frame_nb)),
                           conf.indent_lvl*" ")
    """Return given template formated and repeated frame_nb time."""
    frames = "".join(repeat(template, frame_nb))

    return textwrap.indent(frames, conf.indent_lvl*" ")


def cli_parser() -> argparse.ArgumentParser:
    """Create and return an ArgumentParser object."""
    parser = argparse.ArgumentParser(description="Setup easily tex document.")

    parser.add_argument("-p", "--presentation",


@@ 35,11 39,13 @@ def cli_parser() -> argparse.ArgumentParser:
    return parser


def write_output(filename: str, content: str) -> str:
def write_as_tex(filename: str, text: str) -> str:
    """Create a file named filename and write given text into it."""
    filename += ".tex"
    with open(filename, "w") as f:
        f.write(content)
        f.write(text)

    return f"Generate {filename}."
    return f"Generate '{filename}'."


if __name__ == "__main__":


@@ 49,21 55,23 @@ if __name__ == "__main__":
        title, frame_nb = args.presentation

        packages = render_packages(conf.packages)
        content = render_frames(conf.frame_template, int(frame_nb))
        content = render_frames(conf.Template.frame, int(frame_nb))

        template = string.Template(conf.presentation_template)
        template = string.Template(conf.Template.presentation)
        result = template.substitute(theme=conf.beamer_theme,
                                     packages=packages,
                                     title=title,
                                     author=conf.author,
                                     content=content)

        print(write_as_tex(title, result))
    elif args.document:
        title = args.document
        packages = render_packages(conf.packages)

        template = string.Template(conf.article_template)
        template = string.Template(conf.Template.article)
        result = template.substitute(packages=packages,
                                     title=title,
                                     author=conf.author,)

    print(write_output(f"{title}.tex", result))
        print(write_as_tex(args.document, result))