logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

html.lua (6889B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- HTML LPeg lexer.
  3. local lexer = lexer
  4. local word_match = lexer.word_match
  5. local P, S = lpeg.P, lpeg.S
  6. local lex = lexer.new(..., {no_user_word_lists = true})
  7. -- Comments.
  8. lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.range('<!--', '-->')))
  9. -- Doctype.
  10. lex:add_rule('doctype',
  11. lex:tag(lexer.TAG .. '.doctype', lexer.range('<!' * word_match('doctype', true), '>')))
  12. -- Tags.
  13. local paired_tag = lex:tag(lexer.TAG, lex:word_match(lexer.TAG, true))
  14. local single_tag = lex:tag(lexer.TAG .. '.single', lex:word_match(lexer.TAG .. '.single', true))
  15. local known_tag = paired_tag + single_tag
  16. local unknown_tag = lex:tag(lexer.TAG .. '.unknown', (lexer.alnum + '-')^1)
  17. local tag = lex:tag(lexer.TAG .. '.chars', '<' * P('/')^-1) * (known_tag + unknown_tag) * -P(':')
  18. lex:add_rule('tag', tag)
  19. -- Closing tags.
  20. local tag_close = lex:tag(lexer.TAG .. '.chars', P('/')^-1 * '>')
  21. lex:add_rule('tag_close', tag_close)
  22. -- Equals.
  23. -- TODO: performance is terrible on large files.
  24. local in_tag = P(function(input, index)
  25. local before = input:sub(1, index - 1)
  26. local s, e = before:find('<[^>]-$'), before:find('>[^<]-$')
  27. if s and e then return s > e end
  28. if s then return true end
  29. return input:find('^[^<]->', index) ~= nil
  30. end)
  31. local equals = lex:tag(lexer.OPERATOR, '=') -- * in_tag
  32. -- lex:add_rule('equals', equals)
  33. -- Attributes.
  34. local known_attribute = lex:tag(lexer.ATTRIBUTE, lex:word_match(lexer.ATTRIBUTE, true) +
  35. ((P('data-') + 'aria-') * (lexer.alnum + '-')^1))
  36. local unknown_attribute = lex:tag(lexer.ATTRIBUTE .. '.unknown', (lexer.alnum + '-')^1)
  37. local ws = lex:get_rule('whitespace')
  38. local attribute_eq = (known_attribute + unknown_attribute) * ws^-1 * equals
  39. lex:add_rule('attribute', attribute_eq)
  40. -- Strings.
  41. local string = lex:tag(lexer.STRING, lexer.after_set('=', lexer.range("'") + lexer.range('"')))
  42. lex:add_rule('string', string)
  43. -- Numbers.
  44. local number = lex:tag(lexer.NUMBER, lexer.dec_num * P('%')^-1)
  45. lex:add_rule('number', lexer.after_set('=', number)) -- *in_tag)
  46. -- Entities.
  47. lex:add_rule('entity', lex:tag(lexer.CONSTANT_BUILTIN .. '.entity',
  48. '&' * (lexer.any - lexer.space - ';')^1 * ';'))
  49. -- Fold points.
  50. local function disambiguate_lt(text, pos, line, s)
  51. if line:find('/>', s) then
  52. return 0
  53. elseif line:find('^</', s) then
  54. return -1
  55. else
  56. return 1
  57. end
  58. end
  59. lex:add_fold_point(lexer.TAG .. '.chars', '<', disambiguate_lt)
  60. lex:add_fold_point(lexer.COMMENT, '<!--', '-->')
  61. -- Tags that start embedded languages.
  62. -- Export these patterns for proxy lexers (e.g. ASP) that need them.
  63. lex.embed_start_tag = tag * (ws * attribute_eq * ws^-1 * string)^0 * ws^-1 * tag_close
  64. lex.embed_end_tag = tag * tag_close
  65. -- Embedded CSS (<style type="text/css"> ... </style>).
  66. local css = lexer.load('css')
  67. local style_tag = word_match('style', true)
  68. local css_start_rule = #('<' * style_tag * ('>' + P(function(input, index)
  69. if input:find('^%s+type%s*=%s*(["\'])text/css%1', index) then return true end
  70. end))) * lex.embed_start_tag
  71. local css_end_rule = #('</' * style_tag * '>') * lex.embed_end_tag
  72. lex:embed(css, css_start_rule, css_end_rule)
  73. -- Embedded CSS in style="" attribute.
  74. local style = lexer.load('css', 'css.style')
  75. css_start_rule = #(P('style') * lexer.space^0 * '=') * attribute_eq * ws^-1 *
  76. lex:tag(lexer.STRING, '"')
  77. css_end_rule = lex:tag(lexer.STRING, '"')
  78. lex:embed(style, css_start_rule, css_end_rule) -- only double-quotes for now
  79. -- Embedded JavaScript (<script type="text/javascript"> ... </script>).
  80. local js = lexer.load('javascript')
  81. local script_tag = word_match('script', true)
  82. local js_start_rule = #('<' * script_tag * ('>' + P(function(input, index)
  83. if input:find('^%s+type%s*=%s*(["\'])text/javascript%1', index) then return true end
  84. end))) * lex.embed_start_tag
  85. local js_end_rule = #('</' * script_tag * '>') * lex.embed_end_tag
  86. lex:embed(js, js_start_rule, js_end_rule)
  87. -- Embedded CoffeeScript (<script type="text/coffeescript"> ... </script>).
  88. local cs = lexer.load('coffeescript')
  89. script_tag = word_match('script', true)
  90. local cs_start_rule = #('<' * script_tag * P(function(input, index)
  91. if input:find('^[^>]+type%s*=%s*(["\'])text/coffeescript%1', index) then return true end
  92. end)) * lex.embed_start_tag
  93. local cs_end_rule = #('</' * script_tag * '>') * lex.embed_end_tag
  94. lex:embed(cs, cs_start_rule, cs_end_rule)
  95. -- Word lists.
  96. lex:set_word_list(lexer.TAG .. '.single', {
  97. 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta',
  98. 'param', 'source', 'track', 'wbr'
  99. })
  100. lex:set_word_list(lexer.TAG, {
  101. 'a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'blockquote', 'body',
  102. 'button', 'canvas', 'caption', 'cite', 'code', 'colgroup', 'content', 'data', 'datalist', 'dd',
  103. 'decorator', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'element', 'em', 'fieldset',
  104. 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header',
  105. 'html', 'i', 'iframe', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'menu',
  106. 'menuitem', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p',
  107. 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'shadow',
  108. 'small', 'spacer', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td',
  109. 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'u', 'ul', 'var', 'video'
  110. })
  111. lex:set_word_list(lexer.ATTRIBUTE, {
  112. 'accept', 'accept-charset', 'accesskey', 'action', 'align', 'alt', 'async', 'autocomplete',
  113. 'autofocus', 'autoplay', 'bgcolor', 'border', 'buffered', 'challenge', 'charset', 'checked',
  114. 'cite', 'class', 'code', 'codebase', 'color', 'cols', 'colspan', 'content', 'contenteditable',
  115. 'contextmenu', 'controls', 'coords', 'data', 'data-', 'datetime', 'default', 'defer', 'dir',
  116. 'dirname', 'disabled', 'download', 'draggable', 'dropzone', 'enctype', 'for', 'form', 'headers',
  117. 'height', 'hidden', 'high', 'href', 'hreflang', 'http-equiv', 'icon', 'id', 'ismap', 'itemprop',
  118. 'keytype', 'kind', 'label', 'lang', 'language', 'list', 'loop', 'low', 'manifest', 'max',
  119. 'maxlength', 'media', 'method', 'min', 'multiple', 'name', 'novalidate', 'open', 'optimum',
  120. 'pattern', 'ping', 'placeholder', 'poster', 'preload', 'pubdate', 'radiogroup', 'readonly', 'rel',
  121. 'required', 'reversed', 'role', 'rows', 'rowspan', 'sandbox', 'scope', 'scoped', 'seamless',
  122. 'selected', 'shape', 'size', 'sizes', 'span', 'spellcheck', 'src', 'srcdoc', 'srclang', 'start',
  123. 'step', 'style', 'summary', 'tabindex', 'target', 'title', 'type', 'usemap', 'value', 'width',
  124. 'wrap'
  125. })
  126. lexer.property['scintillua.comment'] = '<!--|-->'
  127. lexer.property['scintillua.angle.braces'] = '1'
  128. lexer.property['scintillua.word.chars'] =
  129. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-'
  130. return lex