stringify.ml 8.5 KB

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