|
1 | 1 | open Odoc_utils |
2 | 2 | open Odoc_parser |
3 | 3 |
|
4 | | -let rec nestable_block_element = function |
5 | | - | { |
6 | | - Loc.location = _; |
7 | | - value = `Verbatim _ | `Modules _ | `Math_block _ | `Media _ | `Paragraph _; |
8 | | - } -> |
9 | | - () |
10 | | - | { |
11 | | - location = _; |
12 | | - value = `Code_block { Ast.content = { value; location }; _ }; |
13 | | - } -> |
14 | | - Format.printf "#%d \"%s\"\n" (location.start.line + 1) location.file; |
15 | | - Format.printf "%s" |
16 | | - (String.v ~len:(location.start.column + 1) (fun _ -> ' ')); |
17 | | - Format.printf "%s" value |
18 | | - | { location = _; value = `List (_, _, l) } -> |
19 | | - List.iter (List.iter nestable_block_element) l |
20 | | - | { location = _; value = `Table ((table, _), _) } -> |
| 4 | +let tags_included_in_names names tags = |
| 5 | + let fields = String.fields ~empty:false tags in |
| 6 | + List.exists |
| 7 | + (fun tag -> |
| 8 | + match String.cut ~sep:"=" tag with |
| 9 | + | Some ("name", n) -> List.exists (String.equal n) names |
| 10 | + | _ -> false) |
| 11 | + fields |
| 12 | + |
| 13 | +let needs_extraction names meta = |
| 14 | + let check_language l = String.equal "ocaml" l.Loc.value in |
| 15 | + let check_name tags = |
| 16 | + if List.is_empty names then true |
| 17 | + else |
| 18 | + match tags with |
| 19 | + | None -> false |
| 20 | + | Some tags -> tags_included_in_names names tags.Loc.value |
| 21 | + in |
| 22 | + match meta with |
| 23 | + | None -> false |
| 24 | + | Some meta -> check_language meta.Ast.language && check_name meta.tags |
| 25 | + |
| 26 | +let print oc line_directives location value = |
| 27 | + if line_directives then ( |
| 28 | + Printf.fprintf oc "#%d \"%s\"\n" (location.Loc.start.line + 1) location.file; |
| 29 | + Printf.fprintf oc "%s%s\n" |
| 30 | + (String.v ~len:(location.start.column + 1) (fun _ -> ' ')) |
| 31 | + value) |
| 32 | + else Printf.fprintf oc "%s" value |
| 33 | + |
| 34 | +let rec nestable_block_element line_directives oc names v = |
| 35 | + match v.Loc.value with |
| 36 | + | `Verbatim _ | `Modules _ | `Math_block _ | `Media _ | `Paragraph _ -> () |
| 37 | + | `Code_block { Ast.content = { value; location }; meta; _ } |
| 38 | + when needs_extraction names meta -> |
| 39 | + print oc line_directives location value |
| 40 | + | `Code_block _ -> () |
| 41 | + | `List (_, _, l) -> |
| 42 | + List.iter (List.iter (nestable_block_element line_directives oc names)) l |
| 43 | + | `Table ((table, _), _) -> |
21 | 44 | List.iter |
22 | | - (List.iter (fun (x, _) -> List.iter nestable_block_element x)) |
| 45 | + (List.iter (fun (x, _) -> |
| 46 | + List.iter (nestable_block_element line_directives oc names) x)) |
23 | 47 | table |
24 | 48 |
|
25 | | -and block_element = function |
26 | | - | { |
27 | | - Loc.value = |
28 | | - `Tag |
29 | | - ( `Deprecated l |
30 | | - | `Param (_, l) |
31 | | - | `Raise (_, l) |
32 | | - | `Return l |
33 | | - | `See (_, _, l) |
34 | | - | `Before (_, l) ); |
35 | | - _; |
36 | | - } -> |
37 | | - List.iter nestable_block_element l |
38 | | - | { |
39 | | - Loc.value = |
40 | | - `Tag |
41 | | - ( `Author _ | `Since _ | `Version _ | `Canonical _ | `Inline | `Open |
42 | | - | `Children_order _ | `Toc_status _ | `Order_category _ |
43 | | - | `Short_title _ | `Closed | `Hidden ); |
44 | | - _; |
45 | | - } |
46 | | - | { Loc.value = `Heading _; _ } -> |
| 49 | +and block_element line_directives oc names v = |
| 50 | + match v.Loc.value with |
| 51 | + | `Tag |
| 52 | + ( `Deprecated l |
| 53 | + | `Param (_, l) |
| 54 | + | `Raise (_, l) |
| 55 | + | `Return l |
| 56 | + | `See (_, _, l) |
| 57 | + | `Before (_, l) ) -> |
| 58 | + List.iter (nestable_block_element line_directives oc names) l |
| 59 | + | `Tag |
| 60 | + ( `Author _ | `Since _ | `Version _ | `Canonical _ | `Inline | `Open |
| 61 | + | `Children_order _ | `Toc_status _ | `Order_category _ | `Short_title _ |
| 62 | + | `Closed | `Hidden ) |
| 63 | + | `Heading _ -> |
47 | 64 | () |
48 | | - | { Loc.value = #Ast.nestable_block_element; _ } as x -> |
49 | | - nestable_block_element x |
| 65 | + | #Ast.nestable_block_element as value -> |
| 66 | + nestable_block_element line_directives oc names { v with value } |
50 | 67 |
|
51 | | -let extract ~dst:_ ~input ~names:_ ~line_directives:_ = |
| 68 | +let extract ~dst ~input ~names ~line_directives = |
52 | 69 | let location = |
53 | 70 | { Lexing.pos_fname = input; pos_lnum = 0; pos_bol = 0; pos_cnum = 0 } |
54 | 71 | in |
55 | 72 | let c = Io_utils.read_lines input |> String.concat ~sep:"\n" in |
56 | 73 | let parsed = parse_comment ~location ~text:c in |
57 | 74 | let ast = ast parsed in |
58 | | - List.iter block_element ast |
| 75 | + let go oc = List.iter (block_element line_directives oc names) ast in |
| 76 | + match dst with None -> go stdout | Some dst -> Io_utils.with_open_out dst go |
0 commit comments