Skip to content

Commit

Permalink
format codes
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci authored and tiensonqin committed Feb 28, 2021
1 parent 8e26c9e commit c44f059
Show file tree
Hide file tree
Showing 50 changed files with 4,921 additions and 3,806 deletions.
44 changes: 32 additions & 12 deletions bench/bench.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
open Mldoc.Parser
open Mldoc.Conf

open Core
open Core_bench

let doc_org = load_file "./examples/doc.org"

let syntax_org = load_file "./examples/syntax.org"

let syntax_md = load_file "./examples/syntax.md"

let main () =
Command.run (Bench.make_command [
Bench.Test.create ~name:"Mldoc Org mode parser (doc)" (fun () ->
let config = {toc = true; heading_number = true; keep_line_break = false; format = Org; } in
ignore (parse config doc_org));
Bench.Test.create ~name:"Mldoc Org mode parser (syntax)" (fun () ->
let config = {toc = true; heading_number = true; keep_line_break = false; format = Org; } in
ignore (parse config syntax_org));
Bench.Test.create ~name:"Mldoc Markdown parser" (fun () ->
let config = {toc = true; heading_number = true; keep_line_break = false; format = Markdown; } in
ignore (parse config syntax_md));
])
Command.run
(Bench.make_command
[ Bench.Test.create ~name:"Mldoc Org mode parser (doc)" (fun () ->
let config =
{ toc = true
; heading_number = true
; keep_line_break = false
; format = Org
}
in
ignore (parse config doc_org))
; Bench.Test.create ~name:"Mldoc Org mode parser (syntax)" (fun () ->
let config =
{ toc = true
; heading_number = true
; keep_line_break = false
; format = Org
}
in
ignore (parse config syntax_org))
; Bench.Test.create ~name:"Mldoc Markdown parser" (fun () ->
let config =
{ toc = true
; heading_number = true
; keep_line_break = false
; format = Markdown
}
in
ignore (parse config syntax_md))
])

let () = main ()
6 changes: 3 additions & 3 deletions bench/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(executable
(name bench)
(name bench)
(libraries angstrom mldoc core core_bench))

(alias
(name bench)
(deps bench.exe))
(name bench)
(deps bench.exe))
2 changes: 1 addition & 1 deletion bin/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(executable
(name main)
(name main)
(public_name mldoc)
(libraries angstrom mldoc cmdliner lwt.unix))

Expand Down
61 changes: 39 additions & 22 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@ open Lwt
open Cmdliner

(* stdin *)
let read_lines () =
Lwt_io.read_lines Lwt_io.stdin |> Lwt_stream.to_list
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 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
let format =
match extension with
| ".markdown"
| ".md" ->
Markdown
| _ -> Org
in
let lines =
if filename = "-" then
read_lines ()
else from_file filename
else
from_file filename
in
lines >>= function lines ->
let config = {toc = true; heading_number = true; keep_line_break = false; format; } in
lines >>= function
| lines ->
let config =
{ toc = true; heading_number = true; keep_line_break = false; format }
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
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 export config document fdout in
return result
Expand All @@ -38,11 +55,11 @@ let generate backend output _opts filename =
(* Commonon options *)
let output =
let doc = "Write the generated file to $(docv). " in
Arg.(value & opt string "" & info ["o"; "output"] ~docv:"OUTPUT-FILE" ~doc)
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)
Arg.(value & opt string "html" & info [ "b"; "backend" ] ~docv:"BACKEND" ~doc)

let filename =
let doc = "The input filename to use. (`-` for stdin) " in
Expand All @@ -56,20 +73,21 @@ let options =
Arg.(
value
& opt_all (pair ~sep:'=' string string) []
& info ["x"; "option"] ~docv:"OPTIONS" ~doc)
& info [ "x"; "option" ] ~docv:"OPTIONS" ~doc)

let cmd = Term.(pure generate $ backend $ output $ options $ filename)

let doc = "converts org-mode or markdown files into various formats"

let options =
[]
let options = []

let man =
[ `S "DESCRIPTION"
; `P
"$(tname) can currently converts org-mode or markdown files into other formats such as
HTML." ]
"$(tname) can currently converts org-mode or markdown files into other \
formats such as\n\
\ HTML."
]
@ options

let infos = Term.info "mldoc" ~version:"0" ~doc ~man
Expand All @@ -82,5 +100,4 @@ let main () =

let () =
let _ = Printexc.record_backtrace true in
if not !Sys.interactive then
main ()
if not !Sys.interactive then main ()
9 changes: 5 additions & 4 deletions js/dune
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(executable
(name lib)
(name lib)
(modes js)
(preprocess (pps js_of_ocaml-ppx))
(preprocess
(pps js_of_ocaml-ppx))
(libraries angstrom js_of_ocaml mldoc))

(alias
(name JS)
(deps lib.bc.js))
(name JS)
(deps lib.bc.js))
123 changes: 58 additions & 65 deletions js/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ open Mldoc
open Mldoc.Parser
open Angstrom

let ast_to_json ast =
Type.blocks_to_yojson ast |> Yojson.Safe.to_string
let ast_to_json ast = Type.blocks_to_yojson ast |> Yojson.Safe.to_string

let generate backend config doc output =
let export = Exporters.find backend in
Expand All @@ -13,72 +12,66 @@ let _ =
let open Js_of_ocaml in
Js.export "Mldoc"
(object%js
method parseJson input config_json =
let config_json = Js.to_string config_json in
let config_json = Yojson.Safe.from_string config_json in
match Conf.of_yojson config_json with
| Ok config -> begin
try
let str = Js.to_string input in
parse config str |> ast_to_json |> Js.string
with error ->
print_endline (Printexc.to_string error);
input
end
| Error e ->
Js_of_ocaml.Js.string ("Config error: " ^ e)
method parseJson input config_json =
let config_json = Js.to_string config_json in
let config_json = Yojson.Safe.from_string config_json in
match Conf.of_yojson config_json with
| Ok config -> (
try
let str = Js.to_string input in
parse config str |> ast_to_json |> Js.string
with error ->
print_endline (Printexc.to_string error);
input)
| Error e -> Js_of_ocaml.Js.string ("Config error: " ^ e)

method parseInlineJson input config_json =
let config_json = Js.to_string config_json in
let config_json = Yojson.Safe.from_string config_json in
match Conf.of_yojson config_json with
| Ok config -> begin
let str = Js.to_string input in
match parse_string ~consume:All (Mldoc.Inline.parse config) str with
| Ok result -> Mldoc.Type.inline_list_to_yojson result |> Yojson.Safe.to_string |> Js.string
| Error e ->
print_endline e;
input
end
| Error e ->
Js_of_ocaml.Js.string ("Config error: " ^ e)
method parseInlineJson input config_json =
let config_json = Js.to_string config_json in
let config_json = Yojson.Safe.from_string config_json in
match Conf.of_yojson config_json with
| Ok config -> (
let str = Js.to_string input in
match parse_string ~consume:All (Mldoc.Inline.parse config) str with
| Ok result ->
Mldoc.Type.inline_list_to_yojson result
|> Yojson.Safe.to_string |> Js.string
| Error e ->
print_endline e;
input)
| Error e -> Js_of_ocaml.Js.string ("Config error: " ^ e)

method parseHtml input config_json =
let str = Js.to_string input in
let config_json = Js.to_string config_json in
let buffer = Buffer.create 1024 in
let config_json = Yojson.Safe.from_string config_json in
match Conf.of_yojson config_json with
| Ok config ->
let ast = parse config str in
let document = Document.from_ast None ast in
let _ = Sys_js.set_channel_flusher stdout (fun s ->
Buffer.add_string buffer s
) in
generate "html" config document stdout;
Js_of_ocaml.Js.string (Buffer.contents buffer)
| Error error ->
Js_of_ocaml.Js.string error
method parseHtml input config_json =
let str = Js.to_string input in
let config_json = Js.to_string config_json in
let buffer = Buffer.create 1024 in
let config_json = Yojson.Safe.from_string config_json in
match Conf.of_yojson config_json with
| Ok config ->
let ast = parse config str in
let document = Document.from_ast None ast in
let _ =
Sys_js.set_channel_flusher stdout (fun s ->
Buffer.add_string buffer s)
in
generate "html" config document stdout;
Js_of_ocaml.Js.string (Buffer.contents buffer)
| Error error -> Js_of_ocaml.Js.string error

method anchorLink s =
let s = Js.to_string s in
Js_of_ocaml.Js.string (Heading.anchor_link s)
method anchorLink s =
let s = Js.to_string s in
Js_of_ocaml.Js.string (Heading.anchor_link s)

method timestampToString input =
let str = Js.to_string input in
let json = Yojson.Safe.from_string str in
match Timestamp.of_yojson json with
| Ok t ->
Timestamp.to_string t |> Js.string
| Error error ->
Js_of_ocaml.Js.string error
method timestampToString input =
let str = Js.to_string input in
let json = Yojson.Safe.from_string str in
match Timestamp.of_yojson json with
| Ok t -> Timestamp.to_string t |> Js.string
| Error error -> Js_of_ocaml.Js.string error

method rangeToString input =
let str = Js.to_string input in
let json = Yojson.Safe.from_string str in
match Range.of_yojson json with
| Ok t ->
Range.to_string t |> Js.string
| Error error ->
Js_of_ocaml.Js.string error
method rangeToString input =
let str = Js.to_string input in
let json = Yojson.Safe.from_string str in
match Range.of_yojson json with
| Ok t -> Range.to_string t |> Js.string
| Error error -> Js_of_ocaml.Js.string error
end)
Loading

0 comments on commit c44f059

Please sign in to comment.