comparison curlyq @ 26:3264788aa0c8

Add --tex option.
author David Barts <n5jrn@me.com>
date Sun, 22 Mar 2020 08:32:52 -0700
parents f4cc6d8cafe8
children 70e75dd07e03
comparison
equal deleted inserted replaced
25:d3eb798f7e95 26:3264788aa0c8
28 # For feet/inches/min/sec 28 # For feet/inches/min/sec
29 BACKT = "`" 29 BACKT = "`"
30 FTMIN = "'" 30 FTMIN = "'"
31 INSEC = '"' 31 INSEC = '"'
32 32
33 # For --tex option
34 TEX_SUBST = [ ("---", "—"), ("--", "–"), ("...", "…") ]
35
33 # C l a s s e s 36 # C l a s s e s
34 37
35 class SafeWorkspace(Workspace): 38 class SafeWorkspace(Workspace):
36 def __getitem__(self, key): 39 def __getitem__(self, key):
37 try: 40 try:
50 ws.append(line) 53 ws.append(line)
51 if line == "" or line == "\n": 54 if line == "" or line == "\n":
52 if args.force: uncurl(ws) 55 if args.force: uncurl(ws)
53 curler.feed() 56 curler.feed()
54 if args.backtick: fims(ws) 57 if args.backtick: fims(ws)
58 if args.tex: tex(ws)
55 output_fp.write(str(ws)) 59 output_fp.write(str(ws))
56 ws.clear() 60 ws.clear()
57 if line == "": 61 if line == "":
58 break 62 break
59 63
67 break 71 break
68 ws.append(line) 72 ws.append(line)
69 if args.force: uncurl(ws) 73 if args.force: uncurl(ws)
70 curler.feed() 74 curler.feed()
71 if args.backtick: fims(ws) 75 if args.backtick: fims(ws)
76 if args.tex: tex(ws)
72 output_fp.write(str(ws)) 77 output_fp.write(str(ws))
73 ws.clear() 78 ws.clear()
74 79
75 def html(): 80 def html():
76 global input_fp, output_fp 81 global input_fp, output_fp
96 buf[pos:pos+2] = INSEC 101 buf[pos:pos+2] = INSEC
97 else: 102 else:
98 buf[pos] = FTMIN 103 buf[pos] = FTMIN
99 pos += 1 104 pos += 1
100 105
106 def gsub(buf, old, repl):
107 old = Workspace(old)
108 repl = Workspace(repl)
109 olen = len(old)
110 delta = len(repl)
111 pos = 0
112 while True:
113 pos = buf.find(old, pos)
114 if pos < 0:
115 break
116 buf[pos:pos+olen] = repl
117 pos += delta
118
119 def tex(buf):
120 global TEX_SUBST
121 for i in TEX_SUBST:
122 gsub(buf, i[0], i[1])
123
101 # M a i n P r o g r a m 124 # M a i n P r o g r a m
102 125
103 # Parse arguments 126 # Parse arguments
104 parser = argparse.ArgumentParser( 127 parser = argparse.ArgumentParser(
105 description='Make straight quotes curly.', prog=MYNAME) 128 description='Make straight quotes curly.', prog=MYNAME)
110 group.add_argument("--uncurl", action="store_true", help="Uncurl quotes instead of curling them.") 133 group.add_argument("--uncurl", action="store_true", help="Uncurl quotes instead of curling them.")
111 parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.") 134 parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.")
112 parser.add_argument("--icoding", default="UTF-8", help="Input encoding (default UTF-8).") 135 parser.add_argument("--icoding", default="UTF-8", help="Input encoding (default UTF-8).")
113 parser.add_argument("--inplace", action="store_true", help="Edit file in-place.") 136 parser.add_argument("--inplace", action="store_true", help="Edit file in-place.")
114 parser.add_argument("--ocoding", default="UTF-8", help="Output encoding (default UTF-8).") 137 parser.add_argument("--ocoding", default="UTF-8", help="Output encoding (default UTF-8).")
138 parser.add_argument("--tex", action="store_true", help="TeX/LaTeX style dash and ellipsis substitution.")
115 parser.add_argument("input", nargs="?", help="Input file.") 139 parser.add_argument("input", nargs="?", help="Input file.")
116 parser.add_argument("output", nargs="?", help="Output file.") 140 parser.add_argument("output", nargs="?", help="Output file.")
117 try: 141 try:
118 args = parser.parse_args() 142 args = parser.parse_args()
119 except SystemExit: 143 except SystemExit:
120 sys.exit(2) 144 sys.exit(2)
121 145
122 # Sanity check 146 # Sanity checks
123 if args.html and args.backtick: 147 if args.html and args.backtick:
124 sys.stderr.write(MYNAME + ": --backtick not supported in --html mode\n") 148 sys.stderr.write(MYNAME + ": --backtick not supported in --html mode\n")
149 sys.exit(2)
150 if args.html and args.tex:
151 sys.stderr.write(MYNAME + ": --tex not supported in --html mode\n")
125 sys.exit(2) 152 sys.exit(2)
126 153
127 # Sanity-check codings 154 # Sanity-check codings
128 try: 155 try:
129 codec = codecs.lookup(args.icoding) 156 codec = codecs.lookup(args.icoding)