jade_parse.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. package jade
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. func (t *tree) topParse() {
  11. t.Root = t.newList(t.peek().pos)
  12. var (
  13. ext bool
  14. token = t.nextNonSpace()
  15. )
  16. if token.typ == itemExtends {
  17. ext = true
  18. t.Root.append(t.parseSubFile(token.val))
  19. token = t.nextNonSpace()
  20. }
  21. for {
  22. switch token.typ {
  23. case itemInclude:
  24. t.Root.append(t.parseInclude(token))
  25. case itemBlock, itemBlockPrepend, itemBlockAppend:
  26. if ext {
  27. t.parseBlock(token)
  28. } else {
  29. t.Root.append(t.parseBlock(token))
  30. }
  31. case itemMixin:
  32. t.mixin[token.val] = t.parseMixin(token)
  33. case itemEOF:
  34. return
  35. case itemExtends:
  36. t.errorf(`Declaration of template inheritance ("extends") should be the first thing in the file. There can only be one extends statement per file.`)
  37. case itemError:
  38. t.errorf("%s line: %d\n", token.val, token.line)
  39. default:
  40. if ext {
  41. t.errorf(`Only import, named blocks and mixins can appear at the top level of an extending template`)
  42. }
  43. t.Root.append(t.hub(token))
  44. }
  45. token = t.nextNonSpace()
  46. }
  47. }
  48. func (t *tree) hub(token item) (n node) {
  49. for {
  50. switch token.typ {
  51. case itemDiv:
  52. token.val = "div"
  53. fallthrough
  54. case itemTag, itemTagInline, itemTagVoid, itemTagVoidInline:
  55. return t.parseTag(token)
  56. case itemText, itemComment, itemHTMLTag:
  57. return t.newText(token.pos, []byte(token.val), token.typ)
  58. case itemCode, itemCodeBuffered, itemCodeUnescaped, itemMixinBlock:
  59. return t.newCode(token.pos, token.val, token.typ)
  60. case itemIf, itemUnless:
  61. return t.parseIf(token)
  62. case itemFor, itemEach, itemWhile:
  63. return t.parseFor(token)
  64. case itemCase:
  65. return t.parseCase(token)
  66. case itemBlock, itemBlockPrepend, itemBlockAppend:
  67. return t.parseBlock(token)
  68. case itemMixinCall:
  69. return t.parseMixinUse(token)
  70. case itemInclude:
  71. return t.parseInclude(token)
  72. case itemDoctype:
  73. return t.newDoctype(token.pos, token.val)
  74. case itemFilter:
  75. return t.parseFilter(token)
  76. case itemError:
  77. t.errorf("Error lex: %s line: %d\n", token.val, token.line)
  78. default:
  79. t.errorf(`Error hub(): unexpected token "%s" type "%s"`, token.val, token.typ)
  80. }
  81. }
  82. }
  83. func (t *tree) parseFilter(tk item) node {
  84. var subf, args, text string
  85. Loop:
  86. for {
  87. switch token := t.nextNonSpace(); token.typ {
  88. case itemFilterSubf:
  89. subf = token.val
  90. case itemFilterArgs:
  91. args = strings.Trim(token.val, " \t\r\n")
  92. case itemFilterText:
  93. text = strings.Trim(token.val, " \t\r\n")
  94. default:
  95. break Loop
  96. }
  97. }
  98. t.backup()
  99. switch tk.val {
  100. case "go":
  101. filterGo(subf, args, text)
  102. case "markdown", "markdown-it":
  103. // TODO: filterMarkdown(subf, args, text)
  104. }
  105. return t.newList(tk.pos) // for return nothing
  106. }
  107. func filterGo(subf, args, text string) {
  108. switch subf {
  109. case "func":
  110. goFlt.Name = ""
  111. switch args {
  112. case "name":
  113. goFlt.Name = text
  114. case "arg", "args":
  115. if goFlt.Args != "" {
  116. goFlt.Args += ", " + strings.Trim(text, "()")
  117. } else {
  118. goFlt.Args = strings.Trim(text, "()")
  119. }
  120. default:
  121. fn := strings.Split(text, "(")
  122. if len(fn) == 2 {
  123. goFlt.Name = strings.Trim(fn[0], " \t\n)")
  124. goFlt.Args = strings.Trim(fn[1], " \t\n)")
  125. } else {
  126. log.Fatal(":go:func filter error in " + text)
  127. }
  128. }
  129. case "import":
  130. goFlt.Import = text
  131. }
  132. }
  133. func (t *tree) parseTag(tk item) node {
  134. var (
  135. deep = tk.depth
  136. tag = t.newTag(tk.pos, tk.val, tk.typ)
  137. )
  138. Loop:
  139. for {
  140. switch token := t.nextNonSpace(); {
  141. case token.depth > deep:
  142. if tag.tagType == itemTagVoid || tag.tagType == itemTagVoidInline {
  143. break Loop
  144. }
  145. tag.append(t.hub(token))
  146. case token.depth == deep:
  147. switch token.typ {
  148. case itemClass:
  149. tag.attr("class", `"`+token.val+`"`, false)
  150. case itemID:
  151. tag.attr("id", `"`+token.val+`"`, false)
  152. case itemAttrStart:
  153. t.parseAttributes(tag, `"`)
  154. case itemTagEnd:
  155. tag.tagType = itemTagVoid
  156. return tag
  157. default:
  158. break Loop
  159. }
  160. default:
  161. break Loop
  162. }
  163. }
  164. t.backup()
  165. return tag
  166. }
  167. type pAttr interface {
  168. attr(string, string, bool)
  169. }
  170. func (t *tree) parseAttributes(tag pAttr, qw string) {
  171. var (
  172. aname string
  173. equal bool
  174. unesc bool
  175. stack = make([]string, 0, 4)
  176. )
  177. for {
  178. switch token := t.next(); token.typ {
  179. case itemAttrSpace:
  180. // skip
  181. case itemAttr:
  182. switch {
  183. case aname == "":
  184. aname = token.val
  185. case aname != "" && !equal:
  186. tag.attr(aname, qw+aname+qw, unesc)
  187. aname = token.val
  188. case aname != "" && equal:
  189. stack = append(stack, token.val)
  190. }
  191. case itemAttrEqual, itemAttrEqualUn:
  192. if token.typ == itemAttrEqual {
  193. unesc = false
  194. } else {
  195. unesc = true
  196. }
  197. equal = true
  198. switch len_stack := len(stack); {
  199. case len_stack == 0 && aname != "":
  200. // skip
  201. case len_stack > 1 && aname != "":
  202. tag.attr(aname, strings.Join(stack[:len(stack)-1], " "), unesc)
  203. aname = stack[len(stack)-1]
  204. stack = stack[:0]
  205. case len_stack == 1 && aname == "":
  206. aname = stack[0]
  207. stack = stack[:0]
  208. default:
  209. t.errorf("unexpected '='")
  210. }
  211. case itemAttrComma:
  212. equal = false
  213. switch len_stack := len(stack); {
  214. case len_stack > 0 && aname != "":
  215. tag.attr(aname, strings.Join(stack, " "), unesc)
  216. aname = ""
  217. stack = stack[:0]
  218. case len_stack == 0 && aname != "":
  219. tag.attr(aname, qw+aname+qw, unesc)
  220. aname = ""
  221. }
  222. case itemAttrEnd:
  223. switch len_stack := len(stack); {
  224. case len_stack > 0 && aname != "":
  225. tag.attr(aname, strings.Join(stack, " "), unesc)
  226. case len_stack > 0 && aname == "":
  227. for _, a := range stack {
  228. tag.attr(a, a, unesc)
  229. }
  230. case len_stack == 0 && aname != "":
  231. tag.attr(aname, qw+aname+qw, unesc)
  232. }
  233. return
  234. default:
  235. t.errorf("unexpected %s", token.val)
  236. }
  237. }
  238. }
  239. func (t *tree) parseIf(tk item) node {
  240. var (
  241. deep = tk.depth
  242. cond = t.newCond(tk.pos, tk.val, tk.typ)
  243. )
  244. Loop:
  245. for {
  246. switch token := t.nextNonSpace(); {
  247. case token.depth > deep:
  248. cond.append(t.hub(token))
  249. case token.depth == deep:
  250. switch token.typ {
  251. case itemElse:
  252. ni := t.peek()
  253. if ni.typ == itemIf {
  254. token = t.next()
  255. cond.append(t.newCode(token.pos, token.val, itemElseIf))
  256. } else {
  257. cond.append(t.newCode(token.pos, token.val, token.typ))
  258. }
  259. default:
  260. break Loop
  261. }
  262. default:
  263. break Loop
  264. }
  265. }
  266. t.backup()
  267. return cond
  268. }
  269. func (t *tree) parseFor(tk item) node {
  270. var (
  271. deep = tk.depth
  272. cond = t.newCond(tk.pos, tk.val, tk.typ)
  273. )
  274. Loop:
  275. for {
  276. switch token := t.nextNonSpace(); {
  277. case token.depth > deep:
  278. cond.append(t.hub(token))
  279. case token.depth == deep:
  280. if token.typ == itemElse {
  281. cond.condType = itemForIfNotContain
  282. cond.append(t.newCode(token.pos, token.val, itemForElse))
  283. } else {
  284. break Loop
  285. }
  286. default:
  287. break Loop
  288. }
  289. }
  290. t.backup()
  291. return cond
  292. }
  293. func (t *tree) parseCase(tk item) node {
  294. var (
  295. deep = tk.depth
  296. iCase = t.newCond(tk.pos, tk.val, tk.typ)
  297. )
  298. for {
  299. if token := t.nextNonSpace(); token.depth > deep {
  300. switch token.typ {
  301. case itemCaseWhen, itemCaseDefault:
  302. iCase.append(t.newCode(token.pos, token.val, token.typ))
  303. default:
  304. iCase.append(t.hub(token))
  305. }
  306. } else {
  307. break
  308. }
  309. }
  310. t.backup()
  311. return iCase
  312. }
  313. func (t *tree) parseMixin(tk item) *mixinNode {
  314. var (
  315. deep = tk.depth
  316. mixin = t.newMixin(tk.pos)
  317. )
  318. Loop:
  319. for {
  320. switch token := t.nextNonSpace(); {
  321. case token.depth > deep:
  322. mixin.append(t.hub(token))
  323. case token.depth == deep:
  324. if token.typ == itemAttrStart {
  325. t.parseAttributes(mixin, "")
  326. } else {
  327. break Loop
  328. }
  329. default:
  330. break Loop
  331. }
  332. }
  333. t.backup()
  334. return mixin
  335. }
  336. func (t *tree) parseMixinUse(tk item) node {
  337. tMix, ok := t.mixin[tk.val]
  338. if !ok {
  339. t.errorf(`Mixin "%s" must be declared before use.`, tk.val)
  340. }
  341. var (
  342. deep = tk.depth
  343. mixin = tMix.CopyMixin()
  344. )
  345. Loop:
  346. for {
  347. switch token := t.nextNonSpace(); {
  348. case token.depth > deep:
  349. mixin.appendToBlock(t.hub(token))
  350. case token.depth == deep:
  351. if token.typ == itemAttrStart {
  352. t.parseAttributes(mixin, "")
  353. } else {
  354. break Loop
  355. }
  356. default:
  357. break Loop
  358. }
  359. }
  360. t.backup()
  361. use := len(mixin.AttrName)
  362. tpl := len(tMix.AttrName)
  363. switch {
  364. case use < tpl:
  365. i := 0
  366. diff := tpl - use
  367. mixin.AttrCode = append(mixin.AttrCode, make([]string, diff)...) // Extend slice
  368. for index := 0; index < diff; index++ {
  369. i = tpl - index - 1
  370. if tMix.AttrName[i] != tMix.AttrCode[i] {
  371. mixin.AttrCode[i] = tMix.AttrCode[i]
  372. } else {
  373. mixin.AttrCode[i] = `""`
  374. }
  375. }
  376. mixin.AttrName = tMix.AttrName
  377. case use > tpl:
  378. if tpl <= 0 {
  379. break
  380. }
  381. if strings.HasPrefix(tMix.AttrName[tpl-1], "...") {
  382. mixin.AttrRest = mixin.AttrCode[tpl-1:]
  383. }
  384. mixin.AttrCode = mixin.AttrCode[:tpl]
  385. mixin.AttrName = tMix.AttrName
  386. case use == tpl:
  387. mixin.AttrName = tMix.AttrName
  388. }
  389. return mixin
  390. }
  391. func (t *tree) parseBlock(tk item) *blockNode {
  392. block := t.newList(tk.pos)
  393. for {
  394. token := t.nextNonSpace()
  395. if token.depth > tk.depth {
  396. block.append(t.hub(token))
  397. } else {
  398. break
  399. }
  400. }
  401. t.backup()
  402. var suf string
  403. switch tk.typ {
  404. case itemBlockPrepend:
  405. suf = "_prepend"
  406. case itemBlockAppend:
  407. suf = "_append"
  408. }
  409. t.block[tk.val+suf] = block
  410. return t.newBlock(tk.pos, tk.val, tk.typ)
  411. }
  412. func (t *tree) parseInclude(tk item) *listNode {
  413. switch ext := filepath.Ext(tk.val); ext {
  414. case ".jade", ".pug", "":
  415. return t.parseSubFile(tk.val)
  416. case ".js", ".css", ".tpl", ".md":
  417. ln := t.newList(tk.pos)
  418. ln.append(t.newText(tk.pos, t.read(tk.val), itemText))
  419. return ln
  420. default:
  421. t.errorf(`file extension "%s" is not supported`, ext)
  422. return nil
  423. }
  424. }
  425. func (t *tree) parseSubFile(path string) *listNode {
  426. var incTree = New(t.resolvePath(path))
  427. incTree.block = t.block
  428. incTree.mixin = t.mixin
  429. incTree.fs = t.fs
  430. _, err := incTree.Parse(t.read(path))
  431. if err != nil {
  432. d, _ := os.Getwd()
  433. t.errorf(`in '%s' subtemplate '%s': parseSubFile() error: %s`, d, path, err)
  434. }
  435. return incTree.Root
  436. }
  437. func (t *tree) read(path string) []byte {
  438. path = t.resolvePath(path)
  439. bb, err := readFile(path, t.fs)
  440. if os.IsNotExist(err) {
  441. if ext := filepath.Ext(path); ext == "" {
  442. if _, er := os.Stat(path + ".jade"); os.IsNotExist(er) {
  443. if _, er = os.Stat(path + ".pug"); os.IsNotExist(er) {
  444. wd, _ := os.Getwd()
  445. t.errorf("in '%s' subtemplate '%s': file path error: '.jade' or '.pug' file required", wd, path)
  446. } else {
  447. ext = ".pug"
  448. }
  449. } else {
  450. ext = ".jade"
  451. }
  452. bb, err = readFile(path+ext, t.fs)
  453. }
  454. }
  455. if err != nil {
  456. wd, _ := os.Getwd()
  457. t.errorf(`%s work dir: %s `, err, wd)
  458. }
  459. return bb
  460. }
  461. func (t *tree) resolvePath(path string) string {
  462. currentTmplDir, _ := filepath.Split(t.Name)
  463. path = filepath.Join(currentTmplDir, path)
  464. return filepath.ToSlash(path)
  465. }
  466. func readFile(fname string, fs http.FileSystem) ([]byte, error) {
  467. if fs == nil {
  468. return ReadFunc(fname)
  469. }
  470. file, err := fs.Open(fname)
  471. if err != nil {
  472. return nil, err
  473. }
  474. defer file.Close()
  475. return ioutil.ReadAll(file)
  476. }