Mercurial > cgi-bin > hgweb.cgi > curlyq
comparison curlyq @ 23:dc30266d4d5b
Add --backtick mode.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 15 Jan 2020 09:06:55 -0800 |
parents | a771878f6cf4 |
children | f4cc6d8cafe8 |
comparison
equal
deleted
inserted
replaced
22:a771878f6cf4 | 23:dc30266d4d5b |
---|---|
23 CODECS_TO_NAME = {} | 23 CODECS_TO_NAME = {} |
24 for i in [ "UTF-8", "UTF-16", "UTF-16LE", "UTF-16BE", "UTF-32", "UTF-32LE", "UTF-32BE" ]: | 24 for i in [ "UTF-8", "UTF-16", "UTF-16LE", "UTF-16BE", "UTF-32", "UTF-32LE", "UTF-32BE" ]: |
25 CODECS_TO_NAME[codecs.lookup(i)] = i | 25 CODECS_TO_NAME[codecs.lookup(i)] = i |
26 del i | 26 del i |
27 | 27 |
28 # For feet/inches/min/sec | |
29 BACKT = "`" | |
30 FTMIN = "'" | |
31 INSEC = '"' | |
32 | |
28 # C l a s s e s | 33 # C l a s s e s |
29 | 34 |
30 class SafeWorkspace(Workspace): | 35 class SafeWorkspace(Workspace): |
31 def __getitem__(self, key): | 36 def __getitem__(self, key): |
32 try: | 37 try: |
44 line = input_fp.readline() | 49 line = input_fp.readline() |
45 ws.append(line) | 50 ws.append(line) |
46 if line == "" or line == "\n": | 51 if line == "" or line == "\n": |
47 if args.force: uncurl(ws) | 52 if args.force: uncurl(ws) |
48 curler.feed() | 53 curler.feed() |
54 if args.backtick: fims(ws) | |
49 output_fp.write(str(ws)) | 55 output_fp.write(str(ws)) |
50 ws.clear() | 56 ws.clear() |
51 if line == "": | 57 if line == "": |
52 break | 58 break |
53 | 59 |
60 if line == "": | 66 if line == "": |
61 break | 67 break |
62 ws.append(line) | 68 ws.append(line) |
63 if args.force: uncurl(ws) | 69 if args.force: uncurl(ws) |
64 curler.feed() | 70 curler.feed() |
71 if args.backtick: fims(ws) | |
65 output_fp.write(str(ws)) | 72 output_fp.write(str(ws)) |
66 ws.clear() | 73 ws.clear() |
67 | 74 |
68 def html(): | 75 def html(): |
69 global input_fp, output_fp | 76 global input_fp, output_fp |
77 global input_fp, output_fp | 84 global input_fp, output_fp |
78 ws = SafeWorkspace(input_fp.read()) | 85 ws = SafeWorkspace(input_fp.read()) |
79 uncurl(ws) | 86 uncurl(ws) |
80 output_fp.write(str(ws)) | 87 output_fp.write(str(ws)) |
81 | 88 |
89 def fims(buf): | |
90 pos = 0 | |
91 while True: | |
92 pos = buf.find(BACKT) | |
93 if pos < 0: | |
94 break | |
95 if buf[pos+1] == BACKT: | |
96 buf[pos:pos+2] = INSEC | |
97 else: | |
98 buf[pos] = FTMIN | |
99 pos += 1 | |
100 | |
82 # M a i n P r o g r a m | 101 # M a i n P r o g r a m |
83 | 102 |
84 # Parse arguments | 103 # Parse arguments |
85 parser = argparse.ArgumentParser( | 104 parser = argparse.ArgumentParser( |
86 description='Make straight quotes curly.', prog=MYNAME) | 105 description='Make straight quotes curly.', prog=MYNAME) |
87 group = parser.add_mutually_exclusive_group() | 106 group = parser.add_mutually_exclusive_group() |
107 parser.add_argument("--backtick", action="store_true", help="Use backticks on input for ft/in/min/sec.") | |
88 group.add_argument("--flowed", action="store_true", help="Input is flowed text.") | 108 group.add_argument("--flowed", action="store_true", help="Input is flowed text.") |
89 group.add_argument("--html", action="store_true", help="Input is HTML.") | 109 group.add_argument("--html", action="store_true", help="Input is HTML.") |
90 group.add_argument("--uncurl", action="store_true", help="Uncurl quotes instead of curling them.") | 110 group.add_argument("--uncurl", action="store_true", help="Uncurl quotes instead of curling them.") |
91 parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.") | 111 parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.") |
92 parser.add_argument("--icoding", default="UTF-8", help="Input encoding (default UTF-8).") | 112 parser.add_argument("--icoding", default="UTF-8", help="Input encoding (default UTF-8).") |
95 parser.add_argument("input", nargs="?", help="Input file.") | 115 parser.add_argument("input", nargs="?", help="Input file.") |
96 parser.add_argument("output", nargs="?", help="Output file.") | 116 parser.add_argument("output", nargs="?", help="Output file.") |
97 try: | 117 try: |
98 args = parser.parse_args() | 118 args = parser.parse_args() |
99 except SystemExit: | 119 except SystemExit: |
120 sys.exit(2) | |
121 | |
122 # Sanity check | |
123 if args.html and args.backtick: | |
124 sys.stderr.write(MYNAME + ": --backtick not supported in --html mode\n") | |
100 sys.exit(2) | 125 sys.exit(2) |
101 | 126 |
102 # Sanity-check codings | 127 # Sanity-check codings |
103 try: | 128 try: |
104 codec = codecs.lookup(args.icoding) | 129 codec = codecs.lookup(args.icoding) |