Skip to content

Commit

Permalink
Adding markdown part
Browse files Browse the repository at this point in the history
  • Loading branch information
mhjd committed Jun 18, 2024
1 parent c8f3ac5 commit 5fba9e5
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 9 deletions.
6 changes: 3 additions & 3 deletions html_of_wiki.opam
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "a static website generator for software projects"
synopsis: "A wikicreole to HTML compiler"
description:
"a static website generator for software projects, using wikicreole syntax"
"html_of_wiki is a static website generator used by the Ocsigen project to manage its online documentation."
maintainer: [
"The ocsigen team <[email protected]>" "Leo Valais <[email protected]>"
]
Expand All @@ -20,7 +20,7 @@ depends: [
"re"
"base64"
"reason"
"tyxml" {<= "4.4.0"}
"tyxml"
]
build: [
["dune" "subst"] {pinned}
Expand Down
9 changes: 7 additions & 2 deletions ohow-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ OPTIONS
Writes the HTML into the given file. Always overwrites. Overrides
--print.

--out-language=LANGUAGE (absent=html)
The output language for the export. Can be 'md' for Markdown or
'html' for HTML.

-p, --print
Print the HTML to stdout.

Expand All @@ -118,7 +122,8 @@ OPTIONS
-r, --hl, --headless
Produces raw HTML without head tag and not inside a body tag.

--root=DIR (absent=/home/hugo/html_of_wiki/_build/default)
--root=DIR
(absent=/home/bibou/Documents/stage/html_of_wiki/_build/default)
Use the given root directory.

-t FILE, --template=FILE
Expand All @@ -134,7 +139,7 @@ COMMON OPTIONS
Show version information.

EXIT STATUS
ohow exits with the following status:
ohow exits with:

0 on success.

Expand Down
10 changes: 8 additions & 2 deletions src/ohow/cli.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
open Import

let out_language_cmd =
let doc = "The output language for the export. Can be 'md' for Markdown or 'html' for HTML." in
Cmdliner.Arg.(
value & opt string "html" & info [ "out-language" ] ~docv:"LANGUAGE" ~doc)

let file_cmd =
let doc = "A wikicreole file to convert to HTML." in
Cmdliner.Arg.(non_empty & pos_all file [] & info [] ~docv:"FILE" ~doc)
Expand Down Expand Up @@ -158,7 +163,7 @@ let info_cmd =
Cmd.info "ohow" ~version:"v2.0" ~doc ~man)

let register_options k print headless outfile project root manual api
default_subproject images assets template csw docversions local files =
default_subproject images assets template csw docversions local out_language files =
let open Operators in
let suffix = if local then ".html" else "" in
let pretty = local in
Expand Down Expand Up @@ -188,6 +193,7 @@ let register_options k print headless outfile project root manual api
; csw
; docversions
; local
; out_language
; files
}
in
Expand All @@ -199,7 +205,7 @@ let run main =
const (register_options main)
$ print_cmd $ headless_cmd $ outfile_cmd $ project_cmd $ root_cmd
$ manual_cmd $ api_cmd $ default_subproject_cmd $ img_cmd $ assets_cmd
$ template_cmd $ csw_cmd $ docversions_cmd $ local_cmd $ file_cmd)
$ template_cmd $ csw_cmd $ docversions_cmd $ local_cmd $ out_language_cmd $ file_cmd)
in
let command = Cmdliner.Cmd.v info_cmd ohow_cmd in
exit Cmdliner.Cmd.(eval command)
1 change: 1 addition & 0 deletions src/ohow/global.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type cli_options =
; assets : string option
; template : string option
; csw : string list
; out_language : string
; docversions : string list
}

Expand Down
1 change: 1 addition & 0 deletions src/ohow/global.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type cli_options =
; assets : string option
; template : string option
; csw : string list
; out_language : string
; docversions : string list
}

Expand Down
143 changes: 143 additions & 0 deletions src/ohow/markdown_builder.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
module rec MarkdownBuilder : Wikicreole.Builder with type param = unit and type flow = string = struct
type href = string
type param = unit
type phrasing_without_interactive = string
type phrasing = string
type flow = string
type flow_without_interactive = string
type uo_list = string

let chars s = s
let strong_elem _ l = "**" ^ String.concat "" l ^ "**"
let em_elem _ l = "_" ^ String.concat "" l ^ "_"
let br_elem _ = " \n"
let img_elem _ href alt = "![ " ^ alt ^ " ](" ^ href ^ ")"
let tt_elem _ l = "`" ^ String.concat "" l ^ "`"
let monospace_elem _ l = "`" ^ String.concat "" l ^ "`"
let underlined_elem _ l = "<u>" ^ String.concat "" l ^ "</u>"
let linethrough_elem _ l = "~~" ^ String.concat "" l ^ "~~"
let subscripted_elem _ l = "<sub>" ^ String.concat "" l ^ "</sub>"
let superscripted_elem _ l = "<sup>" ^ String.concat "" l ^ "</sup>"
let nbsp = "&nbsp;"
let endash = "&ndash;"
let emdash = "&mdash;"
let a_elem_phrasing _ href l = "[" ^ String.concat "" l ^ "](" ^ href ^ ")"
let a_elem_flow _ href l = "[" ^ String.concat "" l ^ "](" ^ href ^ ")"

let make_href _ url _ = url
let string_of_href href = href

let p_elem _ l = (* "debut p" ^ *) String.concat "" l (*^ " fin p \n" *)
let pre_elem _ l = "```\n" ^ String.concat "\n" l ^ "\n```\n"
let h1_elem _ l = "# " ^ String.concat "" l ^ "\n\n"
let h2_elem _ l = "## " ^ String.concat "" l ^ "\n\n"
let h3_elem _ l = "### " ^ String.concat "" l ^ "\n\n"
let h4_elem _ l = "#### " ^ String.concat "" l ^ "\n\n"
let h5_elem _ l = "##### " ^ String.concat "" l ^ "\n\n"
let h6_elem _ l = "###### " ^ String.concat "" l ^ "\n\n"
let section_elem _ l = String.concat "" l

let ul_elem _ l = String.concat "" (List.map (fun (item, _, _) -> "- " ^ String.concat "" item) l) ^ "\n"
let ol_elem _ l = String.concat "" (List.mapi (fun i (item, _, _) -> string_of_int (i + 1) ^ ". " ^ String.concat "" item) l)

let dl_elem _ l = String.concat "" (List.map (fun (is_title, item, _) -> if is_title then "**" ^ List.hd item ^ "**\n" else ": " ^ String.concat "" item ^ "\n") l) ^ "\n"
let hr_elem _ = "---\n"

let table_elem _ l =
let rows = List.map (fun (row, _) -> "| " ^ String.concat " | " (List.map (fun (_, _, cell) -> String.concat "" cell) row) ^ " |\n") l in
String.concat "" rows

let phrasing s = s
let flow s = s
let list s = s

let error s = "~~" ^ s ^ "~~"

type plugin_content =
[ `Flow5_link of (href * Wikicreole.attribs * flow_without_interactive)
| `Phrasing_link of (href * Wikicreole.attribs * phrasing_without_interactive)
| `Flow5 of flow
| `Phrasing_without_interactive of phrasing_without_interactive ]

let plugin = MarkdownPlugin.plugin

let plugin_action _ _ _ = fun _ _ _ -> ()
let link_action _ _ _ _ _ = ()
let href_action _ _ _ _ _ = ()
end

and MarkdownPlugin : sig
val plugin : string -> (Wikicreole.plugin_resolver option * (unit -> Wikicreole.attribs -> string option -> MarkdownBuilder.plugin_content))
end = struct
let plugin name =
match name with
| "header" ->
(None, fun _ _ content ->
let content = match content with Some c -> c | None -> "" in
let processed_content = Wikicreole.from_string () (module MarkdownBuilder) content in
`Phrasing_without_interactive (String.concat "" processed_content ^ "\n\n"))
| "div" ->
(None, fun _ attribs content ->
let class_attr = match List.assoc_opt "class" attribs with
| Some c -> c
| None -> ""
in
let content = match content with Some c -> c | None -> "" in
let processed_content = Wikicreole.from_string () (module MarkdownBuilder) content in
`Flow5 ("<div class=\"" ^ class_attr ^ "\">" ^ String.concat "" processed_content ^ "</div>"))
| "section" ->
(None, fun _ attribs content ->
let class_attr = match List.assoc_opt "class" attribs with
| Some c -> c
| None -> ""
in
let content = match content with Some c -> c | None -> "" in
let processed_content = Wikicreole.from_string () (module MarkdownBuilder) content in
`Flow5 ("<section class=\"" ^ class_attr ^ "\">" ^ String.concat "" processed_content ^ "</section>"))
| "span" ->
(None, fun _ attribs content ->
let class_attr = List.assoc "class" attribs in
let content = match content with Some c -> c | None -> "" in
let processed_content = Wikicreole.from_string () (module MarkdownBuilder) content in
`Phrasing_without_interactive ("<span class=\"" ^ class_attr ^ "\">" ^ String.concat "" processed_content ^ "</span>"))
| "outline" ->
(None, fun _ _ content ->
let content = match content with Some c -> c | None -> "" in
let processed_content = Wikicreole.from_string () (module MarkdownBuilder) content in
`Flow5 ("<div id=\"overview\" class=\"ocsimore_outline\">" ^ String.concat "" processed_content ^ "</div>"))
| "a_manual" | "a_api" | "a_api_type" | "a_api_code" | "a_file" ->
(None, fun _ attribs content ->
let href = match List.assoc_opt "href" attribs with
| Some h -> h
| None -> "#"
in
let text = match content with Some c -> c | None -> href in
`Phrasing_without_interactive ("[" ^ text ^ "](" ^ href ^ ")"))
| "a_img" ->
(None, fun _ attribs content ->
let src = List.assoc "src" attribs in
let alt = match content with Some c -> c | None -> "" in
`Phrasing_without_interactive ("![ " ^ alt ^ " ](" ^ src ^ ")"))
| "code" ->
(None, fun _ attribs content ->
let language = match List.assoc_opt "language" attribs with
| Some lang -> lang
| None -> "text"
in
let class_attr = match List.assoc_opt "class" attribs with
| Some c -> " ." ^ c
| None -> ""
in
let content = match content with Some c -> c | None -> "" in
`Phrasing_without_interactive ("```{" ^ "." ^ language ^ class_attr ^ "}\n" ^ content ^ "\n```"))
(* | default_case -> *)
(* print_string ("Handling default case : " ^ default_case ^ " \n"); *)
| _ ->
(None, fun _ _ _ -> `Phrasing_without_interactive "")
end

let print_markdown l =
print_string (String.concat "" l)

let markdown_from_string s =
print_string (String.concat "" (Wikicreole.from_string ~sectioning:true () (module MarkdownBuilder) s))
Empty file added src/ohow/markdown_plugin.ml
Empty file.
18 changes: 16 additions & 2 deletions src/ohow/ohow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

open Import
open Operators
open Markdown_builder

let build_page file content =
let rec flatten elt =
Expand Down Expand Up @@ -94,8 +95,19 @@ let get_output_channel output_channel file =

let process_file opts output_channel file =
Global.with_current_file file (fun () ->
get_output_channel output_channel file
|> ohow ~indent:opts.Global.pretty file)
let oc = get_output_channel output_channel file in
match opts.Global.out_language with
| "md" ->
let content = read_file file in
markdown_from_string content
| "html" ->
ohow ~indent:opts.Global.pretty file oc
| _ ->
ohow ~indent:opts.Global.pretty file oc
)
(* Global.with_current_file file (fun () ->
* get_output_channel output_channel file
* |> ohow ~indent:opts.Global.pretty file) *)

let init_extensions () =
Wiki_ext.init ();
Expand All @@ -122,6 +134,7 @@ let main
; csw
; docversions
; local
; out_language
; files
} =
if not (List.for_all Sys.file_exists files)
Expand Down Expand Up @@ -152,6 +165,7 @@ let main
; csw
; docversions
; local
; out_language
; files
}
in
Expand Down
1 change: 1 addition & 0 deletions src/ohow/wikicreole.mll
Original file line number Diff line number Diff line change
Expand Up @@ -1193,3 +1193,4 @@ let from_string (type param) (type flow) ?(sectioning = false) param builder s =
Wikicreole.from_lexbuf_no_preempt sectioning param (Lexing.from_string s)

}

0 comments on commit 5fba9e5

Please sign in to comment.