config.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package jade
  2. import "io/ioutil"
  3. //go:generate stringer -type=itemType,NodeType -trimprefix=item -output=config_string.go
  4. var TabSize = 4
  5. var ReadFunc = ioutil.ReadFile
  6. var (
  7. golang_mode = false
  8. tag__bgn = "<%s%s>"
  9. tag__end = "</%s>"
  10. tag__void = "<%s%s/>"
  11. tag__arg_esc = ` %s="{{ print %s }}"`
  12. tag__arg_une = ` %s="{{ print %s }}"`
  13. tag__arg_str = ` %s="%s"`
  14. tag__arg_add = `%s " " %s`
  15. tag__arg_bgn = ""
  16. tag__arg_end = ""
  17. cond__if = "{{ if %s }}"
  18. cond__unless = "{{ if not %s }}"
  19. cond__case = "{{/* switch %s */}}"
  20. cond__while = "{{ range %s }}"
  21. cond__for = "{{/* %s, %s */}}{{ range %s }}"
  22. cond__end = "{{ end }}"
  23. cond__for_if = "{{ if gt len %s 0 }}{{/* %s, %s */}}{{ range %s }}"
  24. code__for_else = "{{ end }}{{ else }}"
  25. code__longcode = "{{/* %s */}}"
  26. code__buffered = "{{ %s }}"
  27. code__unescaped = "{{ %s }}"
  28. code__else = "{{ else }}"
  29. code__else_if = "{{ else if %s }}"
  30. code__case_when = "{{/* case %s: */}}"
  31. code__case_def = "{{/* default: */}}"
  32. code__mix_block = "{{/* block */}}"
  33. text__str = "%s"
  34. text__comment = "<!--%s -->"
  35. mixin__bgn = "\n%s"
  36. mixin__end = ""
  37. mixin__var_bgn = ""
  38. mixin__var = "{{ $%s := %s }}"
  39. mixin__var_rest = "{{ $%s := %#v }}"
  40. mixin__var_end = "\n"
  41. mixin__var_block_bgn = ""
  42. mixin__var_block = ""
  43. mixin__var_block_end = ""
  44. )
  45. type ReplaseTokens struct {
  46. GolangMode bool
  47. TagBgn string
  48. TagEnd string
  49. TagVoid string
  50. TagArgEsc string
  51. TagArgUne string
  52. TagArgStr string
  53. TagArgAdd string
  54. TagArgBgn string
  55. TagArgEnd string
  56. CondIf string
  57. CondUnless string
  58. CondCase string
  59. CondWhile string
  60. CondFor string
  61. CondEnd string
  62. CondForIf string
  63. CodeForElse string
  64. CodeLongcode string
  65. CodeBuffered string
  66. CodeUnescaped string
  67. CodeElse string
  68. CodeElseIf string
  69. CodeCaseWhen string
  70. CodeCaseDef string
  71. CodeMixBlock string
  72. TextStr string
  73. TextComment string
  74. MixinBgn string
  75. MixinEnd string
  76. MixinVarBgn string
  77. MixinVar string
  78. MixinVarRest string
  79. MixinVarEnd string
  80. MixinVarBlockBgn string
  81. MixinVarBlock string
  82. MixinVarBlockEnd string
  83. }
  84. func Config(c ReplaseTokens) {
  85. golang_mode = c.GolangMode
  86. if c.TagBgn != "" {
  87. tag__bgn = c.TagBgn
  88. }
  89. if c.TagEnd != "" {
  90. tag__end = c.TagEnd
  91. }
  92. if c.TagVoid != "" {
  93. tag__void = c.TagVoid
  94. }
  95. if c.TagArgEsc != "" {
  96. tag__arg_esc = c.TagArgEsc
  97. }
  98. if c.TagArgUne != "" {
  99. tag__arg_une = c.TagArgUne
  100. }
  101. if c.TagArgStr != "" {
  102. tag__arg_str = c.TagArgStr
  103. }
  104. if c.TagArgAdd != "" {
  105. tag__arg_add = c.TagArgAdd
  106. }
  107. if c.TagArgBgn != "" {
  108. tag__arg_bgn = c.TagArgBgn
  109. }
  110. if c.TagArgEnd != "" {
  111. tag__arg_end = c.TagArgEnd
  112. }
  113. if c.CondIf != "" {
  114. cond__if = c.CondIf
  115. }
  116. if c.CondUnless != "" {
  117. cond__unless = c.CondUnless
  118. }
  119. if c.CondCase != "" {
  120. cond__case = c.CondCase
  121. }
  122. if c.CondWhile != "" {
  123. cond__while = c.CondWhile
  124. }
  125. if c.CondFor != "" {
  126. cond__for = c.CondFor
  127. }
  128. if c.CondEnd != "" {
  129. cond__end = c.CondEnd
  130. }
  131. if c.CondForIf != "" {
  132. cond__for_if = c.CondForIf
  133. }
  134. if c.CodeForElse != "" {
  135. code__for_else = c.CodeForElse
  136. }
  137. if c.CodeLongcode != "" {
  138. code__longcode = c.CodeLongcode
  139. }
  140. if c.CodeBuffered != "" {
  141. code__buffered = c.CodeBuffered
  142. }
  143. if c.CodeUnescaped != "" {
  144. code__unescaped = c.CodeUnescaped
  145. }
  146. if c.CodeElse != "" {
  147. code__else = c.CodeElse
  148. }
  149. if c.CodeElseIf != "" {
  150. code__else_if = c.CodeElseIf
  151. }
  152. if c.CodeCaseWhen != "" {
  153. code__case_when = c.CodeCaseWhen
  154. }
  155. if c.CodeCaseDef != "" {
  156. code__case_def = c.CodeCaseDef
  157. }
  158. if c.CodeMixBlock != "" {
  159. code__mix_block = c.CodeMixBlock
  160. }
  161. if c.TextStr != "" {
  162. text__str = c.TextStr
  163. }
  164. if c.TextComment != "" {
  165. text__comment = c.TextComment
  166. }
  167. if c.MixinBgn != "" {
  168. mixin__bgn = c.MixinBgn
  169. }
  170. if c.MixinEnd != "" {
  171. mixin__end = c.MixinEnd
  172. }
  173. if c.MixinVarBgn != "" {
  174. mixin__var_bgn = c.MixinVarBgn
  175. }
  176. if c.MixinVar != "" {
  177. mixin__var = c.MixinVar
  178. }
  179. if c.MixinVarRest != "" {
  180. mixin__var_rest = c.MixinVarRest
  181. }
  182. if c.MixinVarEnd != "" {
  183. mixin__var_end = c.MixinVarEnd
  184. }
  185. if c.MixinVarBlockBgn != "" {
  186. mixin__var_block_bgn = c.MixinVarBlockBgn
  187. }
  188. if c.MixinVarBlock != "" {
  189. mixin__var_block = c.MixinVarBlock
  190. }
  191. if c.MixinVarBlockEnd != "" {
  192. mixin__var_block_end = c.MixinVarBlockEnd
  193. }
  194. }
  195. //
  196. type goFilter struct {
  197. Name, Args, Import string
  198. }
  199. var goFlt goFilter
  200. // global variable access (goFlt)
  201. func UseGoFilter() *goFilter { return &goFlt }
  202. //
  203. type itemType int8
  204. const (
  205. itemError itemType = iota // error occurred; value is text of error
  206. itemEOF
  207. itemEndL
  208. itemIdent
  209. itemEmptyLine // empty line
  210. itemText // plain text
  211. itemComment // html comment
  212. itemHTMLTag // html <tag>
  213. itemDoctype // Doctype tag
  214. itemDiv // html div for . or #
  215. itemTag // html tag
  216. itemTagInline // inline tags
  217. itemTagEnd // for <tag />
  218. itemTagVoid // self-closing tags
  219. itemTagVoidInline // inline + self-closing tags
  220. itemID // id attribute
  221. itemClass // class attribute
  222. itemAttrStart
  223. itemAttrEnd
  224. itemAttr
  225. itemAttrSpace
  226. itemAttrComma
  227. itemAttrEqual
  228. itemAttrEqualUn
  229. itemFilter
  230. itemFilterSubf
  231. itemFilterArgs
  232. itemFilterText
  233. // itemKeyword // used only to delimit the keywords
  234. itemInclude
  235. itemExtends
  236. itemBlock
  237. itemBlockAppend
  238. itemBlockPrepend
  239. itemMixin
  240. itemMixinCall
  241. itemMixinBlock
  242. itemCode
  243. itemCodeBuffered
  244. itemCodeUnescaped
  245. itemIf
  246. itemElse
  247. itemElseIf
  248. itemUnless
  249. itemEach
  250. itemWhile
  251. itemFor
  252. itemForIfNotContain
  253. itemForElse
  254. itemCase
  255. itemCaseWhen
  256. itemCaseDefault
  257. )
  258. var key = map[string]itemType{
  259. "include": itemInclude,
  260. "extends": itemExtends,
  261. "block": itemBlock,
  262. "append": itemBlockAppend,
  263. "prepend": itemBlockPrepend,
  264. "mixin": itemMixin,
  265. "if": itemIf,
  266. "else": itemElse,
  267. "unless": itemUnless,
  268. "for": itemFor,
  269. "each": itemEach,
  270. "while": itemWhile,
  271. "case": itemCase,
  272. "when": itemCaseWhen,
  273. "default": itemCaseDefault,
  274. "doctype": itemDoctype,
  275. "a": itemTagInline,
  276. "abbr": itemTagInline,
  277. "acronym": itemTagInline,
  278. "b": itemTagInline,
  279. "code": itemTagInline,
  280. "em": itemTagInline,
  281. "font": itemTagInline,
  282. "i": itemTagInline,
  283. "ins": itemTagInline,
  284. "kbd": itemTagInline,
  285. "map": itemTagInline,
  286. "samp": itemTagInline,
  287. "small": itemTagInline,
  288. "span": itemTagInline,
  289. "strong": itemTagInline,
  290. "sub": itemTagInline,
  291. "sup": itemTagInline,
  292. "area": itemTagVoid,
  293. "base": itemTagVoid,
  294. "col": itemTagVoid,
  295. "command": itemTagVoid,
  296. "embed": itemTagVoid,
  297. "hr": itemTagVoid,
  298. "input": itemTagVoid,
  299. "keygen": itemTagVoid,
  300. "link": itemTagVoid,
  301. "meta": itemTagVoid,
  302. "param": itemTagVoid,
  303. "source": itemTagVoid,
  304. "track": itemTagVoid,
  305. "wbr": itemTagVoid,
  306. "br": itemTagVoidInline,
  307. "img": itemTagVoidInline,
  308. }
  309. //
  310. // nodeType identifies the type of a parse tree node.
  311. type nodeType int8
  312. // Type returns itself and provides an easy default implementation
  313. // for embedding in a Node. Embedded in all non-trivial Nodes.
  314. func (t nodeType) Type() nodeType {
  315. return t
  316. }
  317. const (
  318. nodeText nodeType = iota
  319. nodeList
  320. nodeTag
  321. nodeCode
  322. nodeCond
  323. nodeString
  324. nodeDoctype
  325. nodeMixin
  326. nodeBlock
  327. )