aboutsummaryrefslogtreecommitdiff
path: root/src/smp/__init__.py
blob: 5358c29c6fe3db773c2fe4d2ff7a34059afad1d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import smp.macro_processor
import smp.builtins

__all__ = [
    "smp.macro_processor",
    "smp.builtins",
]


def repl(macro_processor):
    print("=Skaldpress Macro Processor (REPL)")
    # print(" type \"quit\" to exit");
    print("NOT IMPLEMENTED")
    # Intend to use code.InteractiveConsole or code.InteractiveInterpreter
    # as well as the readline library


def read_stdin(macro_processor):
    import sys

    data = sys.stdin.read()
    macro_processor._enter_file_frame("[stdin]", 0, None)
    res = macro_processor.process_input(data, file="[stdin]")
    print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━", file=sys.stderr)
    print(res)


def main():
    import argparse
    import sys
    from skaldpress.main import parse_keyval_args

    parser = argparse.ArgumentParser(description="Process some arguments.")
    parser.add_argument(
        "-D", nargs="+", metavar="key=value", default=[], action="extend"
    )
    parser.add_argument(
        "file", nargs="?", default=None, help='Input file or "-" for stdin'
    )
    args = parser.parse_args()
    args.D = parse_keyval_args(args.D)

    macro_processor_state = smp.macro_processor.MacroProcessorState()
    macro_processor = macro_processor_state.macro_processor()

    for key, value in args.D.items():
        macro_processor.define_macro(key, value)

    if not sys.stdin.isatty() or (args.file == "-"):
        read_stdin(macro_processor)
        sys.exit(0)

    if args.file is None:
        repl(macro_processor)
        sys.exit(0)

    res = smp.builtins._smp_builtin_read(macro_processor, args.file)
    print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━", file=sys.stderr)
    print(res)

    for warning in macro_processor.warnings:
        print(f"\u001b[33m{warning}\u001b[0m", file=sys.stderr)