stringify.ml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. let string_of_num n =
  11. if float_of_int (int_of_float n) = n
  12. then string_of_int (int_of_float n)
  13. else string_of_float n
  14. (*
  15. * Pretty-printing
  16. *)
  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 rec minify_expr = function
  129. | Concat values -> cat " " minify_expr values
  130. | Function (name, arg) -> name ^ "(" ^ minify_expr arg ^ ")"
  131. | Unary (op, opnd) -> op ^ minify_expr opnd
  132. | Nary (",", opnds) -> cat "," minify_expr opnds
  133. | Nary (op, opnds) -> cat op minify_expr opnds
  134. | expr -> string_of_expr expr
  135. let minify_declaration (name, value, important) =
  136. let imp = if important then "!important" else "" in
  137. name ^ ":" ^ minify_expr value ^ imp
  138. let rec minify_selector = stringify_selector ""
  139. let minify_media_feature = function
  140. | (feature, None) -> "(" ^ feature ^ ")"
  141. | (feature, Some value) -> "(" ^ feature ^ ":" ^ minify_expr value ^ ")"
  142. let minify_media_query query =
  143. let features_str = cat "and " minify_media_feature in
  144. match query with
  145. | (None, None, features) -> features_str features
  146. | (None, Some mtype, features) -> mtype ^ " and " ^ features_str features
  147. | (Some pre, Some mtype, features) ->
  148. pre ^ " " ^ mtype ^ " and " ^ features_str features
  149. | _ -> string_of_media_query query
  150. let rec minify_statement = function
  151. | Ruleset (selectors, decls) ->
  152. cat "," minify_selector selectors ^
  153. "{" ^ cat ";" minify_declaration decls ^ "}"
  154. | Media (queries, rulesets) ->
  155. "@media" ^ prefix_space (cat "," minify_media_query queries) ^
  156. "{" ^ cat "" minify_statement rulesets ^ "}"
  157. | Import (target, []) ->
  158. "@import " ^ string_of_expr target ^ ";"
  159. | Import (target, queries) ->
  160. "@import " ^ string_of_expr target ^ " " ^
  161. cat "," string_of_media_query queries ^ ";"
  162. | Page (None, decls) ->
  163. "@page{" ^ cat ";" minify_declaration decls ^ "}"
  164. | Page (Some pseudo, decls) ->
  165. "@page :" ^ pseudo ^ "{" ^ cat ";" minify_declaration decls ^ "}"
  166. | Font_face decls ->
  167. let minify_descriptor_declaration (name, value) =
  168. name ^ ":" ^ string_of_expr value
  169. in
  170. "@font-face{" ^ cat ";" minify_descriptor_declaration decls ^ "}"
  171. | Keyframes (prefix, id, rules) ->
  172. let minify_keyframe_ruleset (expr, decls) =
  173. minify_expr expr ^ "{" ^ cat ";" minify_declaration decls ^ "}"
  174. in
  175. "@" ^ prefix ^ "keyframes " ^ id ^
  176. "{" ^ cat "" minify_keyframe_ruleset rules ^ "}"
  177. | Supports (condition, statements) ->
  178. "@supports " ^ stringify_condition "" condition ^
  179. "{" ^ cat "" minify_statement statements ^ "}"
  180. | statement -> string_of_statement statement
  181. let minify_stylesheet = cat "" minify_statement
  182. (*
  183. * Stringify any AST node in a box
  184. *)
  185. let string_of_box = function
  186. | Expr expr ->
  187. string_of_expr expr
  188. | Declaration declaration ->
  189. string_of_declaration declaration
  190. | Selector selector ->
  191. string_of_selector selector
  192. | Media_expr media_expr ->
  193. string_of_media_expr media_expr
  194. | Media_query media_query ->
  195. string_of_media_query media_query
  196. | Descriptor_declaration descriptor_declaration ->
  197. string_of_descriptor_declaration descriptor_declaration
  198. | Keyframe_ruleset keyframe_ruleset ->
  199. string_of_keyframe_ruleset keyframe_ruleset
  200. | Condition condition ->
  201. string_of_condition condition
  202. | Statement statement ->
  203. string_of_statement statement
  204. | Stylesheet stylesheet ->
  205. string_of_stylesheet stylesheet
  206. | Clear ->
  207. "<clear>"
  208. | _ ->
  209. raise (Invalid_argument "box")