stringify.ml 8.3 KB

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