Use a lua filter rather than a massive python script
This commit is contained in:
parent
631e948548
commit
55603857da
5 changed files with 39 additions and 79 deletions
|
@ -6,12 +6,6 @@ jobs:
|
|||
working_directory: ~/tex-template
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
name: Install Dependencies
|
||||
command: apt-get install -y python-flake8
|
||||
- run:
|
||||
name: Lint
|
||||
command: flake8 build.py --ignore=E128,E501
|
||||
- run:
|
||||
name: Test build
|
||||
command: python3 build.py test/main.md
|
||||
command: bash build.sh test/main.md
|
||||
|
|
68
build.py
68
build.py
|
@ -1,68 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import pathlib
|
||||
import os
|
||||
import subprocess
|
||||
import re
|
||||
import tempfile
|
||||
|
||||
|
||||
CWD = pathlib.Path(os.getcwd())
|
||||
BASE_DIR = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
|
||||
TEMPLATE_FILE = BASE_DIR / 'main.tex'
|
||||
OUTPUT_FILE = CWD / 'output.pdf'
|
||||
WORDCOUNT_TEMPLATE = BASE_DIR / 'wordcount.yaml'
|
||||
|
||||
|
||||
def build(input_file, additional_args=None):
|
||||
build_args = ['pandoc', str(input_file)]
|
||||
|
||||
if additional_args is not None:
|
||||
build_args.extend(additional_args)
|
||||
|
||||
build_args.extend([
|
||||
'--template',
|
||||
str(TEMPLATE_FILE),
|
||||
'-o',
|
||||
str(OUTPUT_FILE)
|
||||
])
|
||||
subprocess.check_output(build_args, cwd=str(BASE_DIR))
|
||||
|
||||
|
||||
def get_word_count():
|
||||
words = subprocess.check_output([
|
||||
'pdftotext', str(OUTPUT_FILE), '-'
|
||||
], cwd=str(BASE_DIR))
|
||||
return len(re.findall(r'\w+', words.decode()))
|
||||
|
||||
|
||||
def write_wordcount():
|
||||
_, 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.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_help = True
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parse_args()
|
||||
input_file = args.input.resolve()
|
||||
operating_dir = input_file.parents[0]
|
||||
context_file = operating_dir.joinpath('context.yaml')
|
||||
if not input_file.is_file():
|
||||
raise ValueError("Failed to find {}".format(input_file))
|
||||
additional_args = []
|
||||
if context_file.exists():
|
||||
additional_args.append(str(context_file))
|
||||
build(input_file, additional_args)
|
||||
additional_args.append(write_wordcount())
|
||||
build(input_file, additional_args)
|
15
build.sh
Executable file
15
build.sh
Executable file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
SCRIPT=`realpath $0`
|
||||
SCRIPTPATH=`dirname $SCRIPT`
|
||||
|
||||
INPUT_FILE=`realpath $1`
|
||||
OUTPUT_FILE=output.pdf
|
||||
|
||||
PANDOC_ARGS="$INPUT_FILE --template $SCRIPTPATH/main.tex -o $OUTPUT_FILE"
|
||||
|
||||
echo "Building document..."
|
||||
pandoc $PANDOC_ARGS
|
||||
|
||||
echo "Running second pass..."
|
||||
pandoc $PANDOC_ARGS --lua-filter=second-pass.lua
|
23
second-pass.lua
Normal file
23
second-pass.lua
Normal file
|
@ -0,0 +1,23 @@
|
|||
function file_exists(name)
|
||||
local f=io.open(name,"r")
|
||||
if f~=nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
|
||||
local function countWords()
|
||||
local rawText = pandoc.pipe("pdftotext", {"output.pdf", "-"}, "")
|
||||
return pandoc.pipe("wc", {"-w"}, rawText)
|
||||
end
|
||||
|
||||
|
||||
return {
|
||||
{
|
||||
Meta = function(meta)
|
||||
if file_exists("output.pdf") then
|
||||
meta["wordcount"] = countWords()
|
||||
meta["secondpass"] = true
|
||||
end
|
||||
return meta
|
||||
end,
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
---
|
||||
wordcount: {wordcount}
|
||||
secondpass: true
|
||||
...
|
Reference in a new issue