Inject auto-calculated wordcount
This commit is contained in:
parent
0874fc9d5d
commit
b8f4c28be1
4 changed files with 34 additions and 10 deletions
33
build.py
33
build.py
|
@ -3,6 +3,8 @@ import argparse
|
||||||
import pathlib
|
import pathlib
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import re
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
|
||||||
CWD = pathlib.Path(os.getcwd())
|
CWD = pathlib.Path(os.getcwd())
|
||||||
|
@ -10,15 +12,18 @@ BASE_DIR = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
|
||||||
TEMPLATE_FILE = BASE_DIR / 'main.tex'
|
TEMPLATE_FILE = BASE_DIR / 'main.tex'
|
||||||
OUTPUT_FILE = CWD / 'output.pdf'
|
OUTPUT_FILE = CWD / 'output.pdf'
|
||||||
CONTEXT_FILE = CWD / 'context.yaml'
|
CONTEXT_FILE = CWD / 'context.yaml'
|
||||||
BUILD_PASSES = 2
|
WORDCOUNT_TEMPLATE = BASE_DIR / 'wordcount.yaml'
|
||||||
|
|
||||||
|
|
||||||
def build(input_file: pathlib.Path):
|
def build(input_file: pathlib.Path, additional_args=None):
|
||||||
build_args = ['pandoc', str(input_file)]
|
build_args = ['pandoc', str(input_file)]
|
||||||
|
|
||||||
if CONTEXT_FILE.exists():
|
if CONTEXT_FILE.exists():
|
||||||
build_args.append(str(CONTEXT_FILE))
|
build_args.append(str(CONTEXT_FILE))
|
||||||
|
|
||||||
|
if additional_args is not None:
|
||||||
|
build_args.extend(additional_args)
|
||||||
|
|
||||||
build_args.extend([
|
build_args.extend([
|
||||||
'--template',
|
'--template',
|
||||||
str(TEMPLATE_FILE),
|
str(TEMPLATE_FILE),
|
||||||
|
@ -28,12 +33,28 @@ def build(input_file: pathlib.Path):
|
||||||
return subprocess.run(build_args, cwd=str(BASE_DIR), check=True)
|
return subprocess.run(build_args, cwd=str(BASE_DIR), check=True)
|
||||||
|
|
||||||
|
|
||||||
def parse_args(args=None):
|
def get_word_count() -> int:
|
||||||
|
words = subprocess.check_output([
|
||||||
|
'pdftotext', str(OUTPUT_FILE), '-'
|
||||||
|
], cwd=str(BASE_DIR))
|
||||||
|
return len(re.findall(r'\w+', words.decode()))
|
||||||
|
|
||||||
|
|
||||||
|
def write_wordcount() -> pathlib.Path:
|
||||||
|
_, out_file_path = tempfile.mkstemp(suffix='.yaml')
|
||||||
|
wordcount = get_word_count()
|
||||||
|
with open(out_file_path, 'w') as out_file:
|
||||||
|
with WORDCOUNT_TEMPLATE.open() as template:
|
||||||
|
out_file.write(template.read().format(wordcount=wordcount))
|
||||||
|
return pathlib.Path(out_file_path)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-v", "--verbose", help="Set verbosity level (repeat argument)", action="count", default=0)
|
parser.add_argument("-v", "--verbose", help="Set verbosity level (repeat argument)", action="count", default=0)
|
||||||
parser.add_argument("input", help="Input File", action='store', type=pathlib.Path,)
|
parser.add_argument("input", help="Input File", action='store', type=pathlib.Path,)
|
||||||
parser.add_help = True
|
parser.add_help = True
|
||||||
return parser.parse_args(args=args)
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -41,6 +62,6 @@ if __name__ == '__main__':
|
||||||
input_file = args.input.resolve()
|
input_file = args.input.resolve()
|
||||||
if not input_file.is_file():
|
if not input_file.is_file():
|
||||||
raise ValueError("Failed to find {}".format(input_file))
|
raise ValueError("Failed to find {}".format(input_file))
|
||||||
for i in range(2):
|
|
||||||
print("Building pass {}.".format(i + 1))
|
|
||||||
build(input_file)
|
build(input_file)
|
||||||
|
wordcount_metadata_file = write_wordcount()
|
||||||
|
build(input_file, [str(wordcount_metadata_file)])
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
\pagestyle{fancy}
|
\pagestyle{fancy}
|
||||||
|
|
||||||
\rfoot{$wordcount$ words}
|
\rfoot{\wordcount words}
|
||||||
\cfoot{\thepage\ of \pageref{LastPage}}
|
\cfoot{\thepage\ of \pageref{LastPage}}
|
||||||
\lhead{\thetitle}
|
\lhead{\thetitle}
|
||||||
\rhead{\date}
|
\rhead{\date}
|
||||||
|
|
4
main.tex
4
main.tex
|
@ -1,6 +1,5 @@
|
||||||
\documentclass[12pt,titlepage,a4paper,twoside]{article}
|
\documentclass[12pt,titlepage,a4paper,twoside]{article}
|
||||||
\include{packages}
|
\include{packages}
|
||||||
\include{header-footer}
|
|
||||||
|
|
||||||
\renewcommand{\thesection}{}
|
\renewcommand{\thesection}{}
|
||||||
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}
|
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}
|
||||||
|
@ -13,10 +12,11 @@
|
||||||
|
|
||||||
\title{$title$}
|
\title{$title$}
|
||||||
\newcommand{\subtitle}{$subtitle$}
|
\newcommand{\subtitle}{$subtitle$}
|
||||||
|
\newcommand{\wordcount}{$wordcount$}
|
||||||
\author{Some Author}
|
\author{Some Author}
|
||||||
\date{2018-01-01}
|
\date{2018-01-01}
|
||||||
|
|
||||||
|
\include{header-footer}
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
|
|
||||||
|
|
3
wordcount.yaml
Normal file
3
wordcount.yaml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
wordcount: {wordcount}
|
||||||
|
...
|
Reference in a new issue