forked from logseq/mldoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ml
115 lines (102 loc) · 2.89 KB
/
main.ml
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
open Mldoc
open Mldoc.Parser
open Mldoc.Conf
open Lwt
open Cmdliner
(* stdin *)
let read_lines () = Lwt_io.read_lines Lwt_io.stdin |> Lwt_stream.to_list
(* file *)
let from_file filename = Lwt_io.lines_of_file filename |> Lwt_stream.to_list
let generate backend output _opts filename =
let extension = Filename.extension filename in
let format =
match extension with
| ".markdown"
| ".md" ->
Markdown
| _ -> Org
in
let lines =
if filename = "-" then
read_lines ()
else
from_file filename
in
lines >>= function
| lines ->
let config =
{ toc = true
; parse_outline_only = true
; heading_number = true
; keep_line_break = false
; format
; heading_to_list = true
; exporting_keep_properties = true
; inline_type_with_pos = false
; inline_skip_macro = false
; export_md_indent_style = Dashes
; export_md_remove_options = []
; hiccup_in_block = true
; enable_drawers = true
; parse_marker = true
; parse_priority = true
}
in
let ast = parse config (String.concat "\n" lines) in
let document = Document.from_ast None ast in
let export = Exporters.find backend in
let module E = (val export : Exporter.Exporter) in
let output =
if output = "" then
E.default_filename filename
else
output
in
let fdout =
if output = "-" then
stdout
else
open_out output
in
(* FIXME: parse *)
let result = Exporters.run ~refs:None export config document fdout in
return result
(* Cmd liner part *)
(* Commonon options *)
let output =
let doc = "Write the generated file to $(docv). " in
Arg.(value & opt string "" & info [ "o"; "output" ] ~docv:"OUTPUT-FILE" ~doc)
let backend =
let doc = "Uses $(docv) to generate the output. (`-` for stdout)" in
Arg.(value & opt string "html" & info [ "b"; "backend" ] ~docv:"BACKEND" ~doc)
let filename =
let doc = "The input filename to use. (`-` for stdin) " in
Arg.(value & pos 0 string "-" & info [] ~docv:"FILENAME" ~doc)
let options =
let doc =
"Extra option to use to configure the behaviour. (Can be used multiple \
times)"
in
Arg.(
value
& opt_all (pair ~sep:'=' string string) []
& info [ "x"; "option" ] ~docv:"OPTIONS" ~doc)
let cmd = Term.(const generate $ backend $ output $ options $ filename)
let doc = "converts org-mode or markdown files into various formats"
let options = []
let man =
[ `S "DESCRIPTION"
; `P
"$(tname) can currently converts org-mode or markdown files into other \
formats such as\n\
\ HTML."
]
@ options
let infos = Cmd.info "mldoc" ~version:"0" ~doc ~man
let main () =
match Cmd.v infos cmd |> Cmd.eval_value with
| Ok (`Ok expr) -> Lwt_main.run expr
| _ -> exit 1
let () =
let _ = Printexc.record_backtrace true in
if not !Sys.interactive then main ()