7827408d2369 — Phillip Alday 6 years ago
Markdown diffs with boldface
2 files changed, 102 insertions(+), 0 deletions(-)

M Makefile
A => hg-diff-md.py
M Makefile +9 -0
@@ 18,6 18,7 @@ library ?= /path/to/library
 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 @@ draft: $(paper).pdf
 %.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}
 

          
A => hg-diff-md.py +93 -0
@@ 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 "<nodiff>" in line:
+            skip = True
+        elif "</nodiff>" 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())