diff curlyq @ 3:091c03f1b2e8

Getting it working...
author David Barts <n5jrn@me.com>
date Thu, 26 Dec 2019 19:54:45 -0800
parents
children 7a83e82e65a6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/curlyq	Thu Dec 26 19:54:45 2019 -0800
@@ -0,0 +1,111 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import os, sys
+import argparse
+import codecs
+
+from curlers import TextCurler, HtmlCurler
+from workspace import Workspace, Bounds, Mapping, SegmentedView
+from writer import CODECS_TO_NAME
+
+# V a r i a b l e s
+
+# Name invoked by
+MYNAME = os.path.basename(sys.argv[0])
+
+# Streams
+input_fp = None
+output_fp = None
+
+# F u n c t i o n s
+
+def normal():
+    global input_fp, output_fp
+    with Workspace() as ws:
+        curler = TextCurler(ws)
+        while True:
+            line = input_fp.readline()
+            ws.write(line)
+            if line == "" or line == "\n":
+                curler.feed()
+                output_fp.write(ws.getvalue())
+                ws.clear()
+            if line == "":
+                break
+
+def flowed():
+    global input_fp, output_fp, args
+    with Workspace() as ws:
+        curler = TextCurler(ws)
+        while True:
+            line = input_fp.readline()
+            if line == "":
+                break
+            ws.write(line)
+            curler.feed()
+            output_fp.write(ws.getvalue())
+            ws.clear()
+
+def html():
+    global input_fp, output_fp
+    with Workspace(input_fp.read()) as ws:
+        curler = HtmlCurler(ws)
+        curler.feed()
+        output_fp.write(ws.getvalue())
+
+# M a i n   P r o g r a m
+
+# Parse arguments
+parser = argparse.ArgumentParser(description='Source code character checker.', prog=MYNAME)
+group = parser.add_mutually_exclusive_group()
+group.add_argument("--flowed", action="store_true", help="Input is flowed text.")
+group.add_argument("--html", action="store_true", help="Input is HTML.")
+parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.")
+parser.add_argument("--icoding", default="UTF-8", help="Input encoding.")
+parser.add_argument("--inplace", action="store_true", help="Edit file in-place.")
+parser.add_argument("--ocoding", default="UTF-8", help="Output encoding.")
+parser.add_argument("input", nargs="?", help="Input file.")
+parser.add_argument("output", nargs="?", help="Output file.")
+try:
+    args = parser.parse_args()
+except SystemExit:
+    sys.exit(2)
+
+# Sanity-check codings
+try:
+    codec = codecs.lookup(args.icoding)
+    codec = codecs.lookup(args.ocoding)
+except LookupError as e:
+    sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e))
+    sys.exit(2)
+if not CODECS_TO_NAME.get(codec, "").startswith("UTF-"):
+    sys.stderr.write("{0}: {1!s} output coding does not support Unicode\n".format(MYNAME, args.ocoding))
+    sys.exit(1)
+del codec
+
+# Get streams
+try:
+    if args.input and (not args.output) and args.inplace:
+        args.output = args.input
+        args.input += "~"
+        os.rename(args.input, args.output)
+    if args.input:
+        input_fp = open(args.input, "r", encoding=args.icoding)
+    else:
+        input_fp = open(0, "r", encoding=args.icoding)
+    if args.output:
+        output_fp = open(args.output, "w", encoding=args.ocoding)
+    else:
+        output_fp = open(1, "w", encoding=args.ocoding)
+except (OSError, LookupError) as e:
+    sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e))
+    sys.exit(1)
+
+# Choose our mode
+if args.flowed:
+    flowed()
+elif args.html:
+    html()
+else:
+    normal()