# HG changeset patch # User Phillip Alday # Date 1535557621 -7200 # Wed Aug 29 17:47:01 2018 +0200 # Node ID 7827408d23693e0ce8387ad3a3de60ab5fc3a47e # Parent bd150ad1d8fd81347823ffa01471f969f6e90596 Markdown diffs with boldface diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ paper ?= article mdflags ?= -f markdown-hard_line_breaks+yaml_metadata_block refs ?= $(paper).bib +diffbase ?= 1 draft: $(paper).pdf cp $(paper).pdf $(paper)_`hg tip --template "{date|shortdate}_{node|short}"`.pdf @@ -33,6 +34,14 @@ %.md: %.Rmd echo 'knitr::knit("$*.Rmd")' | R --vanilla +bolddiff: bolddiff.pdf + +bolddiff.md: $(paper).md hg-diff-md.py + hg blame -n $(paper).md | python3 hg-diff-md.py $(diffbase) - $@ + +bolddiff.pdf: bolddiff.md $(refs) $(latex_template) $(deps) + pandoc $(mdflags) $(pdfflags) --template=$(latex_template) --bibliography=$(refs) $< -o $@ + clean: rm -rf $(paper).{pdf,html,odt,docx} diff --git a/hg-diff-md.py b/hg-diff-md.py new file mode 100755 --- /dev/null +++ b/hg-diff-md.py @@ -0,0 +1,93 @@ +#! /bin/env python3 + +import sys +import argparse + + +parser = argparse.ArgumentParser(description='Change boldfance for Pandoc Markdown', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + +parser.add_argument('revision',type=int,default=0, + help='HG revision to find differences to') +parser.add_argument('blame',type=argparse.FileType('r'), + nargs='?',default=sys.stdin, + help='Output from hg blame -n') +parser.add_argument('out',type=argparse.FileType('w'), + nargs='?',default=sys.stdout, + help='Output file') +parser.add_argument('--skip-yaml-block',type=bool,default=True, + help="Don't circumfix a YAML (when located at top of file)") +parser.add_argument('--prefix',type=str,nargs='?',default='**', + help='Prefix changed lines with this symbol') +parser.add_argument('--suffix',type=str,nargs='?',default='**', + help='Suffix changed lines with this symbol') + +def main(argv=None): + args = parser.parse_args(argv) + + lineno = 1 + + if args.skip_yaml_block: + line = args.blame.readline() + rev, line = line.split(': ',maxsplit=1) + print('{}'.format(line.rstrip()),file=args.out) + # this keeps us form spinning uselessly if we hit the end of a file + # without ending the header block + for line in args.blame: + rev, line = line.split(': ',maxsplit=1) + lineno += 1 + print('{}'.format(line.rstrip()),file=args.out) + if line.strip() in ['---', '...']: + break + + #print(lineno,"lines skipped in YAML block") + in_caption = False + skip = False + prefix = args.prefix + suffix = args.suffix + for line in args.blame: + close_env = False + rev, line = line.split(': ',maxsplit=1) + line = line.rstrip() + if "" in line: + skip = True + elif "" in line: + skip = False + + lineno += 1 + #print(int(rev)) + # pandoc assumes everything in a latex block is latex, so we have + # to do latex style formatting in captions + + if r"\caption{" in line: + in_caption = True + if args.prefix in ['*','**']: + suffix = "}" + if args.prefix == '*': + prefix = r'\emph{' + else: + prefix = r'\textbf{' + + if in_caption and line[-1] == '}': + in_caption = False + prefix = args.prefix + suffix = args.suffix + if line == ']': + close_env = True + begin_capt_lbl = r"\caption{" in line or r"\label{" in line + empty = len(line) == 0 + header = "#" in line + modified = int(rev) > args.revision + + if modified and not any([header, empty, begin_capt_lbl, skip, close_env]): + ending = "" + if line[-2:] == '^[': + ending = line[-2:] + line = line[:-2] + print('{}{}{}{}'.format(prefix,line,suffix,ending),file=args.out) + else: + print('{}'.format(line),file=args.out) + + +if __name__ == '__main__': + sys.exit(main())