diff --git a/Makefile b/Makefile index df29639..0eca4c6 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,6 @@ all: build build: odin build src/ -out:TypoML -run: - odin run src/ - clean: rm -f TypoML diff --git a/README.tlm b/README.tlm index 0a19cb3..1684575 100644 --- a/README.tlm +++ b/README.tlm @@ -18,6 +18,31 @@ TypoML reads from stdin and outputs HTML to stdout: cat input.tlm | ./TypoML > output.html ``` +It can also read one or more (added in the order of command line arguments) `.tlm` files: + +``` +./TypoML input.tlm > output.html +``` + +Run `./TypoML --help` for the full list of options. + +!! Options + +- `--title ` - Set the HTML document `` (default: `TypoML`) +- `--plain` - Don't wrap the output in `<html>`, `<head>`, `<body>` and `<style>` tags +- `--no-js` - Don't include the JavaScript variable proxy +- `--no-wrapper` - Shortcut for `--plain` and `--no-js` at once +- `--print-tree` - Print the token tree instead of HTML +- `-h`, `--help` - Show the help text and exit + +Examples: + +``` +./TypoML doc.tlm --title "TITLE" > doc.html +./TypoML doc.tlm --no-wrapper > fragment.html +./TypoML doc.tlm --print-tree +``` + --- !! Syntax diff --git a/preview b/preview index 7c1428f..c08ea14 100755 --- a/preview +++ b/preview @@ -1,39 +1,3 @@ #!/bin/sh -input=$(cat -) -input="${input#odin run src/}" -style="<style>$(cat styles.css)</style>" -doctype='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' -meta='<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' -start="$doctype<html><head><title>TypoML Preview$meta$style" -script=' -' -end="$script" -output="$start$input$end" -echo "$output" > preview.html +cat - > preview.html nohup xdg-open preview.html > /dev/null 2>&1 & diff --git a/src/main.odin b/src/main.odin index d1d9e12..f7a1540 100644 --- a/src/main.odin +++ b/src/main.odin @@ -7,10 +7,26 @@ import "core:fmt" import "core:os" import "core:strings" -read_stdin :: proc() -> []string { - data, err := os.read_entire_file_from_file(os.stdin, context.allocator) - content := string(data) - return strings.split_lines(content) +read_input :: proc(paths: []string) -> []string { + if len(paths) == 0 { + data, err := os.read_entire_file_from_file(os.stdin, context.allocator) + content := string(data) + return strings.split_lines(content) + } + + b := strings.builder_make(context.allocator) + for path, i in paths { + data, err := os.read_entire_file(path, context.allocator) + if err != nil { + fmt.eprintf("typoml: cannot read file '%s': %v\n", path, err) + os.exit(1) + } + if i > 0 { + strings.write_string(&b, "\n") + } + strings.write_string(&b, string(data)) + } + return strings.split_lines(strings.to_string(b)) } print_token :: proc(tok: ^token.Token, depth: int = 0) { @@ -116,8 +132,66 @@ print_token :: proc(tok: ^token.Token, depth: int = 0) { } } -main :: proc() { - lines := read_stdin() - parsed := parser.parse(lines) - renderer.print_html(&parsed) +HELP_TEXT :: `Usage: + TypoML [options] [file...] + +If there is no file provided, TypoML reads from stdin. + +Options: + --title Set the HTML document title (default: "TypoML") + --plain Don't wrap the output in , , and " + title := flags.title + if title == "" { + title = "TypoML" + } + + fmt.printf( + "%s\n%s%s%s\n\n", + doctype, + escape_html(title), + meta, + style, + ) + for child in root.children { + print_html(child) + } + if !flags.no_js { + fmt.print("\n" + TEMPLATE_PROXY_SCRIPT + "\n") + } + fmt.print("\n") +} + +DocumentFlags :: struct { + plain: bool, + no_js: bool, + title: string, + print_tree: bool, + help: bool, +} + print_html :: proc(tok: ^token.Token) { switch tok.type { case token.TokenType.Root: diff --git a/styles.css b/src/renderer/styles.odin similarity index 97% rename from styles.css rename to src/renderer/styles.odin index 0cfa1c8..df57201 100644 --- a/styles.css +++ b/src/renderer/styles.odin @@ -1,3 +1,6 @@ +package renderer + +DEFAULT_STYLE :: ` body { font-family: Georgia, "Times New Roman", Times, serif; color: #e0e0e0; @@ -72,3 +75,4 @@ hr.footnotes-sep { .footnote a { text-decoration: none; } +` diff --git a/test b/test index 0f4ed1c..fc11dd0 100755 --- a/test +++ b/test @@ -1 +1 @@ -clear && make format && cat README.tlm | make run | ./preview +clear && make format && make build && ./TypoML README.tlm --title "TypoML Preview" | ./preview