File: img.ml

package info (click to toggle)
mlpost 0.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,844 kB
  • sloc: ml: 21,094; javascript: 4,047; makefile: 430; ansic: 34; lisp: 19; sh: 15
file content (29 lines) | stat: -rw-r--r-- 798 bytes parent folder | download | duplicates (2)
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
open Ppxlib

module B = Ast_builder.Make (struct
  let loc = Location.none
end)

let img_subst =
  let img_regexp = Str.regexp "^ *@img \\([a-z.]*\\)" in
  Str.global_replace img_regexp "{%html:<img alt=\"\\1\" src=\"img/\\1\"/>%}"

let rewrite_payload p =
  Ast_pattern.(parse (pstr (pstr_eval (estring __) __ ^:: nil))) Location.none p
    (fun s _ -> img_subst s)
  |> fun s -> PStr B.[ pstr_eval (estring s) [] ]

let rewriter =
  object
    inherit Ast_traverse.map

    method! attribute a =
      if a.attr_name.txt = "ocaml.text" || a.attr_name.txt = "ocaml.doc" then
        { a with attr_payload = rewrite_payload a.attr_payload }
      else a
  end

let () =
  let intf = rewriter#signature in
  let impl = rewriter#structure in
  Driver.register_transformation ~intf ~impl "mlpost_doc"