package liquid_parser

  1. Overview
  2. Docs

Source file preprocessor.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
let (~/) = Re2.create_exn

let tag name = "{%-? ?" ^ name ^ " ?-?%}"

let remove exp text = Re2.rewrite_exn exp text ~template:""

let remove_comments inp =
  let tag_comment = ~/(tag "comment" ^ "(.|\n)+?" ^ tag "endcomment") in
  let hash_comment = ~/("{%\\s+?((\\s|\\s+)?#.+)+\\s+?%}") in
  remove tag_comment inp
  |> remove hash_comment

let remove_liquid_comments inp =
  let tag_comment = ~/"^comment(.|\n)+^endcomment" in
  let hash_comment = ~/"#.+" in
  remove tag_comment inp
  |> remove hash_comment

let add_eof text = text ^ "   "

let preprocess text = text |> remove_comments |> add_eof

let raw_open = tag "raw"
let raw_close = tag "endraw"

let is_raw text =
  let exp = ~/("^" ^ raw_open ^ "(.|\n)+") in
  Re2.matches exp text

let until_end_raw text =
  let exp = ~/("(.|\n)+" ^ raw_close) in
  Re2.find_first_exn exp text

let trim_raw_tags text =
  let start_exp = ~/("^" ^ raw_open) in
  let end_exp = ~/(raw_close ^ "$") in
  remove start_exp text
  |> remove end_exp
OCaml

Innovation. Community. Security.