# HG changeset patch # User David Barts # Date 1584891172 25200 # Node ID 3264788aa0c8f771b22f7706d56b744df7285ddd # Parent d3eb798f7e9513aae3d5940e6e4fda901ed6e52e Add --tex option. diff -r d3eb798f7e95 -r 3264788aa0c8 curlyq --- a/curlyq Sun Mar 15 10:52:55 2020 -0700 +++ b/curlyq Sun Mar 22 08:32:52 2020 -0700 @@ -30,6 +30,9 @@ FTMIN = "'" INSEC = '"' +# For --tex option +TEX_SUBST = [ ("---", "—"), ("--", "–"), ("...", "…") ] + # C l a s s e s class SafeWorkspace(Workspace): @@ -52,6 +55,7 @@ if args.force: uncurl(ws) curler.feed() if args.backtick: fims(ws) + if args.tex: tex(ws) output_fp.write(str(ws)) ws.clear() if line == "": @@ -69,6 +73,7 @@ if args.force: uncurl(ws) curler.feed() if args.backtick: fims(ws) + if args.tex: tex(ws) output_fp.write(str(ws)) ws.clear() @@ -98,6 +103,24 @@ buf[pos] = FTMIN pos += 1 +def gsub(buf, old, repl): + old = Workspace(old) + repl = Workspace(repl) + olen = len(old) + delta = len(repl) + pos = 0 + while True: + pos = buf.find(old, pos) + if pos < 0: + break + buf[pos:pos+olen] = repl + pos += delta + +def tex(buf): + global TEX_SUBST + for i in TEX_SUBST: + gsub(buf, i[0], i[1]) + # M a i n P r o g r a m # Parse arguments @@ -112,6 +135,7 @@ parser.add_argument("--icoding", default="UTF-8", help="Input encoding (default UTF-8).") parser.add_argument("--inplace", action="store_true", help="Edit file in-place.") parser.add_argument("--ocoding", default="UTF-8", help="Output encoding (default UTF-8).") +parser.add_argument("--tex", action="store_true", help="TeX/LaTeX style dash and ellipsis substitution.") parser.add_argument("input", nargs="?", help="Input file.") parser.add_argument("output", nargs="?", help="Output file.") try: @@ -119,10 +143,13 @@ except SystemExit: sys.exit(2) -# Sanity check +# Sanity checks if args.html and args.backtick: sys.stderr.write(MYNAME + ": --backtick not supported in --html mode\n") sys.exit(2) +if args.html and args.tex: + sys.stderr.write(MYNAME + ": --tex not supported in --html mode\n") + sys.exit(2) # Sanity-check codings try: