stringify.ml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. open Types
  2. open Util
  3. let tab = " "
  4. let indent = Str.global_replace (Str.regexp "^\\(.\\)") (tab ^ "\\1")
  5. let prefix_space = function "" -> "" | s -> " " ^ s
  6. let rec cat sep fn = function
  7. | [] -> ""
  8. | [hd] -> fn hd
  9. | hd :: tl -> fn hd ^ sep ^ cat sep fn tl
  10. (*
  11. * Pretty-printing
  12. *)
  13. let string_of_num n =
  14. if float_of_int (int_of_float n) = n
  15. then string_of_int (int_of_float n)
  16. else string_of_float n
  17. let rec string_of_expr = function
  18. | Ident id -> id
  19. | Strlit str -> "\"" ^ str ^ "\""
  20. | Uri uri when String.contains uri ')' -> "url(\"" ^ uri ^ "\")"
  21. | Uri uri -> "url(" ^ uri ^ ")"
  22. | Concat values -> cat " " string_of_expr values
  23. | Number (n, None) -> string_of_num n
  24. | Number (n, Some u) -> string_of_num n ^ u
  25. | Function (name, arg) -> name ^ "(" ^ string_of_expr arg ^ ")"
  26. | Hexcolor hex -> "#" ^ hex
  27. | Unary (op, opnd) -> op ^ string_of_expr opnd
  28. | Nary (",", opnds) -> cat ", " string_of_expr opnds
  29. | Nary (op, opnds) -> cat op string_of_expr opnds
  30. | Key_value (key, op, value) -> key ^ op ^ string_of_expr value
  31. let string_of_declaration (name, value, important) =
  32. let imp = if important then " !important" else "" in
  33. name ^ ": " ^ string_of_expr value ^ imp ^ ";"
  34. let rec stringify_selector w selector =
  35. let str = stringify_selector w in
  36. match selector with
  37. | No_element -> ""
  38. | All_elements -> "*"
  39. | Element elem -> elem
  40. | Id (base, id) ->
  41. str base ^ "#" ^ id
  42. | Class (base, cls) ->
  43. str base ^ "." ^ cls
  44. | Attribute (base, attr, None) ->
  45. str base ^ "[" ^ attr ^ "]"
  46. | Attribute (base, attr, Some (op, value)) ->
  47. str base ^ "[" ^ attr ^ w ^ op ^ w ^ string_of_expr value ^ "]"
  48. | Pseudo_class (base, cls, None) ->
  49. str base ^ ":" ^ cls
  50. | Pseudo_class (base, fn, Some args) ->
  51. str base ^ ":" ^ fn ^ "(" ^ cat ("," ^ w) str args ^ ")"
  52. | Pseudo_element (base, elem) ->
  53. str base ^ "::" ^ elem
  54. | Combinator (left, " ", right) ->
  55. str left ^ " " ^ str right
  56. | Combinator (left, com, right) ->
  57. str left ^ w ^ com ^ w ^ str right
  58. let string_of_selector = stringify_selector " "
  59. let string_of_media_expr = function
  60. | (feature, None) -> "(" ^ feature ^ ")"
  61. | (feature, Some value) -> "(" ^ feature ^ ": " ^ string_of_expr value ^ ")"
  62. let string_of_media_query =
  63. let features_str = cat " and " string_of_media_expr in
  64. function
  65. | (None, None, []) -> ""
  66. | (None, Some mtype, []) -> mtype
  67. | (Some pre, Some mtype, []) -> pre ^ " " ^ mtype
  68. | (None, None, features) -> features_str features
  69. | (None, Some mtype, features) -> mtype ^ " and " ^ features_str features
  70. | (Some pre, Some mtype, features) ->
  71. pre ^ " " ^ mtype ^ " and " ^ features_str features
  72. | (Some pre, None, _) ->
  73. failwith "unexpected media query prefix \"" ^ pre ^ "\""
  74. let stringify_condition w c =
  75. let rec transform =
  76. let p c = `Parens (transform c) in
  77. function
  78. | Not c -> `Not (p c)
  79. | And c -> `And (List.map p c)
  80. | Or c -> `Or (List.map p c)
  81. | Decl (name, value) -> `Decl (name, value)
  82. in
  83. let rec str = function
  84. | `Not c -> "not " ^ str c
  85. | `And c -> cat " and " str c
  86. | `Or c -> cat " or " str c
  87. | `Decl (name, value) -> "(" ^ name ^ ":" ^ w ^ string_of_expr value ^ ")"
  88. | `Parens (`Decl _ as d) -> str d
  89. | `Parens c -> "(" ^ str c ^ ")"
  90. in
  91. str (transform c)
  92. let string_of_condition = stringify_condition " "
  93. let block = function "" -> " {}" | body -> " {\n" ^ indent body ^ "\n}"
  94. let string_of_descriptor_declaration (name, value) =
  95. name ^ ": " ^ string_of_expr value ^ ";"
  96. let string_of_keyframe_ruleset (expr, decls) =
  97. string_of_expr expr ^ block (cat "\n" string_of_declaration decls)
  98. let rec string_of_statement = function
  99. | Ruleset (selectors, decls) ->
  100. cat ", " string_of_selector selectors ^
  101. block (cat "\n" string_of_declaration decls)
  102. | Media (queries, rulesets) ->
  103. "@media" ^ prefix_space (cat ", " string_of_media_query queries) ^
  104. block (cat "\n\n" string_of_statement rulesets)
  105. | Import (target, []) ->
  106. "@import " ^ string_of_expr target ^ ";"
  107. | Import (target, queries) ->
  108. "@import " ^ string_of_expr target ^ " " ^ cat ", " string_of_media_query queries ^ ";"
  109. | Charset charset ->
  110. "@charset \"" ^ charset ^ "\";"
  111. | Page (None, decls) ->
  112. "@page" ^ block (cat "\n" string_of_declaration decls)
  113. | Page (Some pseudo, decls) ->
  114. "@page :" ^ pseudo ^ block (cat "\n" string_of_declaration decls)
  115. | Font_face decls ->
  116. "@font-face" ^ block (cat "\n" string_of_descriptor_declaration decls)
  117. | Namespace (None, uri) ->
  118. "@namespace " ^ string_of_expr uri ^ ";"
  119. | Namespace (Some prefix, uri) ->
  120. "@namespace " ^ prefix ^ " " ^ string_of_expr uri ^ ";"
  121. | Keyframes (prefix, id, rules) ->
  122. "@" ^ prefix ^ "keyframes " ^ id ^
  123. block (cat "\n\n" string_of_keyframe_ruleset rules)
  124. | Supports (condition, statements) ->
  125. "@supports " ^ string_of_condition condition ^
  126. block (cat "\n\n" string_of_statement statements)
  127. | Viewport (prefix, decls) ->
  128. "@" ^ prefix ^ "viewport" ^ block (cat "\n" string_of_declaration decls)
  129. let string_of_stylesheet = cat "\n\n" string_of_statement
  130. (*
  131. * Minified stringification
  132. *)
  133. let minify_num n =
  134. (* Round numbers to at most 2 decimal digits *)
  135. let round2 n = floor (100. *. n +. 0.5) /. 100. in
  136. if float_of_int (int_of_float n) = n then
  137. string_of_int (int_of_float n)
  138. else if n < 1.0 && n > -1.0 then
  139. let s = string_of_float (round2 n) in
  140. String.sub s 1 (String.length s - 1)
  141. else
  142. string_of_float (round2 n)
  143. let rec minify_expr = function
  144. | Concat values -> cat " " minify_expr values
  145. | Function (name, arg) -> name ^ "(" ^ minify_expr arg ^ ")"
  146. | Unary (op, opnd) -> op ^ minify_expr opnd
  147. | Nary (",", opnds) -> cat "," minify_expr opnds
  148. | Nary (op, opnds) -> cat op minify_expr opnds
  149. | Number (n, None) -> minify_num n
  150. | Number (n, Some u) -> minify_num n ^ u
  151. | Key_value (key, op, value) -> key ^ op ^ minify_expr value
  152. | expr -> string_of_expr expr
  153. let minify_declaration (name, value, important) =
  154. let imp = if important then "!important" else "" in
  155. name ^ ":" ^ minify_expr value ^ imp
  156. let rec minify_selector = stringify_selector ""
  157. let minify_media_feature = function
  158. | (feature, None) -> "(" ^ feature ^ ")"
  159. | (feature, Some value) -> "(" ^ feature ^ ":" ^ minify_expr value ^ ")"
  160. let minify_media_query query =
  161. let features_str = cat "and " minify_media_feature in
  162. match query with
  163. | (None, None, features) -> features_str features
  164. | (None, Some mtype, features) -> mtype ^ " and " ^ features_str features
  165. | (Some pre, Some mtype, features) ->
  166. pre ^ " " ^ mtype ^ " and " ^ features_str features
  167. | _ -> string_of_media_query query
  168. let rec minify_statement = function
  169. | Ruleset (selectors, decls) ->
  170. cat "," minify_selector selectors ^
  171. "{" ^ cat ";" minify_declaration decls ^ "}"
  172. | Media (queries, rulesets) ->
  173. "@media" ^ prefix_space (cat "," minify_media_query queries) ^
  174. "{" ^ cat "" minify_statement rulesets ^ "}"
  175. | Import (target, []) ->
  176. "@import " ^ minify_expr target ^ ";"
  177. | Import (target, queries) ->
  178. "@import " ^ minify_expr target ^ " " ^
  179. cat "," string_of_media_query queries ^ ";"
  180. | Page (None, decls) ->
  181. "@page{" ^ cat ";" minify_declaration decls ^ "}"
  182. | Page (Some pseudo, decls) ->
  183. "@page :" ^ pseudo ^ "{" ^ cat ";" minify_declaration decls ^ "}"
  184. | Font_face decls ->
  185. let minify_descriptor_declaration (name, value) =
  186. name ^ ":" ^ minify_expr value
  187. in
  188. "@font-face{" ^ cat ";" minify_descriptor_declaration decls ^ "}"
  189. | Keyframes (prefix, id, rules) ->
  190. let minify_keyframe_ruleset (expr, decls) =
  191. minify_expr expr ^ "{" ^ cat ";" minify_declaration decls ^ "}"
  192. in
  193. "@" ^ prefix ^ "keyframes " ^ id ^
  194. "{" ^ cat "" minify_keyframe_ruleset rules ^ "}"
  195. | Supports (condition, statements) ->
  196. "@supports " ^ stringify_condition "" condition ^
  197. "{" ^ cat "" minify_statement statements ^ "}"
  198. | Viewport (prefix, decls) ->
  199. "@" ^ prefix ^ "viewport{" ^ cat ";" minify_declaration decls ^ "}"
  200. | statement -> string_of_statement statement
  201. let minify_stylesheet = cat "" minify_statement
  202. (*
  203. * Stringify any AST node in a box
  204. *)
  205. let string_of_box = function
  206. | Expr expr ->
  207. string_of_expr expr
  208. | Declaration declaration ->
  209. string_of_declaration declaration
  210. | Selector selector ->
  211. string_of_selector selector
  212. | Media_expr media_expr ->
  213. string_of_media_expr media_expr
  214. | Media_query media_query ->
  215. string_of_media_query media_query
  216. | Descriptor_declaration descriptor_declaration ->
  217. string_of_descriptor_declaration descriptor_declaration
  218. | Keyframe_ruleset keyframe_ruleset ->
  219. string_of_keyframe_ruleset keyframe_ruleset
  220. | Condition condition ->
  221. string_of_condition condition
  222. | Statement statement ->
  223. string_of_statement statement
  224. | Stylesheet stylesheet ->
  225. string_of_stylesheet stylesheet
  226. | Clear ->
  227. "<clear>"
  228. | _ ->
  229. raise (Invalid_argument "box")