Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html-text.mjs 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. var htmlText = {
  2. name: 'htmlText',
  3. tokenize: tokenizeHtmlText
  4. }
  5. export default htmlText
  6. import assert from 'assert'
  7. import asciiAlpha from '../character/ascii-alpha.mjs'
  8. import asciiAlphanumeric from '../character/ascii-alphanumeric.mjs'
  9. import codes from '../character/codes.mjs'
  10. import markdownLineEnding from '../character/markdown-line-ending.mjs'
  11. import markdownLineEndingOrSpace from '../character/markdown-line-ending-or-space.mjs'
  12. import markdownSpace from '../character/markdown-space.mjs'
  13. import constants from '../constant/constants.mjs'
  14. import types from '../constant/types.mjs'
  15. import spaceFactory from './factory-space.mjs'
  16. function tokenizeHtmlText(effects, ok, nok) {
  17. var self = this
  18. var marker
  19. var buffer
  20. var index
  21. var returnState
  22. return start
  23. function start(code) {
  24. assert(code === codes.lessThan, 'expected `<`')
  25. effects.enter(types.htmlText)
  26. effects.enter(types.htmlTextData)
  27. effects.consume(code)
  28. return open
  29. }
  30. function open(code) {
  31. if (code === codes.exclamationMark) {
  32. effects.consume(code)
  33. return declarationOpen
  34. }
  35. if (code === codes.slash) {
  36. effects.consume(code)
  37. return tagCloseStart
  38. }
  39. if (code === codes.questionMark) {
  40. effects.consume(code)
  41. return instruction
  42. }
  43. if (asciiAlpha(code)) {
  44. effects.consume(code)
  45. return tagOpen
  46. }
  47. return nok(code)
  48. }
  49. function declarationOpen(code) {
  50. if (code === codes.dash) {
  51. effects.consume(code)
  52. return commentOpen
  53. }
  54. if (code === codes.leftSquareBracket) {
  55. effects.consume(code)
  56. buffer = constants.cdataOpeningString
  57. index = 0
  58. return cdataOpen
  59. }
  60. if (asciiAlpha(code)) {
  61. effects.consume(code)
  62. return declaration
  63. }
  64. return nok(code)
  65. }
  66. function commentOpen(code) {
  67. if (code === codes.dash) {
  68. effects.consume(code)
  69. return commentStart
  70. }
  71. return nok(code)
  72. }
  73. function commentStart(code) {
  74. if (code === codes.eof || code === codes.greaterThan) {
  75. return nok(code)
  76. }
  77. if (code === codes.dash) {
  78. effects.consume(code)
  79. return commentStartDash
  80. }
  81. return comment(code)
  82. }
  83. function commentStartDash(code) {
  84. if (code === codes.eof || code === codes.greaterThan) {
  85. return nok(code)
  86. }
  87. return comment(code)
  88. }
  89. function comment(code) {
  90. if (code === codes.eof) {
  91. return nok(code)
  92. }
  93. if (code === codes.dash) {
  94. effects.consume(code)
  95. return commentClose
  96. }
  97. if (markdownLineEnding(code)) {
  98. returnState = comment
  99. return atLineEnding(code)
  100. }
  101. effects.consume(code)
  102. return comment
  103. }
  104. function commentClose(code) {
  105. if (code === codes.dash) {
  106. effects.consume(code)
  107. return end
  108. }
  109. return comment(code)
  110. }
  111. function cdataOpen(code) {
  112. if (code === buffer.charCodeAt(index++)) {
  113. effects.consume(code)
  114. return index === buffer.length ? cdata : cdataOpen
  115. }
  116. return nok(code)
  117. }
  118. function cdata(code) {
  119. if (code === codes.eof) {
  120. return nok(code)
  121. }
  122. if (code === codes.rightSquareBracket) {
  123. effects.consume(code)
  124. return cdataClose
  125. }
  126. if (markdownLineEnding(code)) {
  127. returnState = cdata
  128. return atLineEnding(code)
  129. }
  130. effects.consume(code)
  131. return cdata
  132. }
  133. function cdataClose(code) {
  134. if (code === codes.rightSquareBracket) {
  135. effects.consume(code)
  136. return cdataEnd
  137. }
  138. return cdata(code)
  139. }
  140. function cdataEnd(code) {
  141. if (code === codes.greaterThan) {
  142. return end(code)
  143. }
  144. if (code === codes.rightSquareBracket) {
  145. effects.consume(code)
  146. return cdataEnd
  147. }
  148. return cdata(code)
  149. }
  150. function declaration(code) {
  151. if (code === codes.eof || code === codes.greaterThan) {
  152. return end(code)
  153. }
  154. if (markdownLineEnding(code)) {
  155. returnState = declaration
  156. return atLineEnding(code)
  157. }
  158. effects.consume(code)
  159. return declaration
  160. }
  161. function instruction(code) {
  162. if (code === codes.eof) {
  163. return nok(code)
  164. }
  165. if (code === codes.questionMark) {
  166. effects.consume(code)
  167. return instructionClose
  168. }
  169. if (markdownLineEnding(code)) {
  170. returnState = instruction
  171. return atLineEnding(code)
  172. }
  173. effects.consume(code)
  174. return instruction
  175. }
  176. function instructionClose(code) {
  177. return code === codes.greaterThan ? end(code) : instruction(code)
  178. }
  179. function tagCloseStart(code) {
  180. if (asciiAlpha(code)) {
  181. effects.consume(code)
  182. return tagClose
  183. }
  184. return nok(code)
  185. }
  186. function tagClose(code) {
  187. if (code === codes.dash || asciiAlphanumeric(code)) {
  188. effects.consume(code)
  189. return tagClose
  190. }
  191. return tagCloseBetween(code)
  192. }
  193. function tagCloseBetween(code) {
  194. if (markdownLineEnding(code)) {
  195. returnState = tagCloseBetween
  196. return atLineEnding(code)
  197. }
  198. if (markdownSpace(code)) {
  199. effects.consume(code)
  200. return tagCloseBetween
  201. }
  202. return end(code)
  203. }
  204. function tagOpen(code) {
  205. if (code === codes.dash || asciiAlphanumeric(code)) {
  206. effects.consume(code)
  207. return tagOpen
  208. }
  209. if (
  210. code === codes.slash ||
  211. code === codes.greaterThan ||
  212. markdownLineEndingOrSpace(code)
  213. ) {
  214. return tagOpenBetween(code)
  215. }
  216. return nok(code)
  217. }
  218. function tagOpenBetween(code) {
  219. if (code === codes.slash) {
  220. effects.consume(code)
  221. return end
  222. }
  223. if (code === codes.colon || code === codes.underscore || asciiAlpha(code)) {
  224. effects.consume(code)
  225. return tagOpenAttributeName
  226. }
  227. if (markdownLineEnding(code)) {
  228. returnState = tagOpenBetween
  229. return atLineEnding(code)
  230. }
  231. if (markdownSpace(code)) {
  232. effects.consume(code)
  233. return tagOpenBetween
  234. }
  235. return end(code)
  236. }
  237. function tagOpenAttributeName(code) {
  238. if (
  239. code === codes.dash ||
  240. code === codes.dot ||
  241. code === codes.colon ||
  242. code === codes.underscore ||
  243. asciiAlphanumeric(code)
  244. ) {
  245. effects.consume(code)
  246. return tagOpenAttributeName
  247. }
  248. return tagOpenAttributeNameAfter(code)
  249. }
  250. function tagOpenAttributeNameAfter(code) {
  251. if (code === codes.equalsTo) {
  252. effects.consume(code)
  253. return tagOpenAttributeValueBefore
  254. }
  255. if (markdownLineEnding(code)) {
  256. returnState = tagOpenAttributeNameAfter
  257. return atLineEnding(code)
  258. }
  259. if (markdownSpace(code)) {
  260. effects.consume(code)
  261. return tagOpenAttributeNameAfter
  262. }
  263. return tagOpenBetween(code)
  264. }
  265. function tagOpenAttributeValueBefore(code) {
  266. if (
  267. code === codes.eof ||
  268. code === codes.lessThan ||
  269. code === codes.equalsTo ||
  270. code === codes.greaterThan ||
  271. code === codes.graveAccent
  272. ) {
  273. return nok(code)
  274. }
  275. if (code === codes.quotationMark || code === codes.apostrophe) {
  276. effects.consume(code)
  277. marker = code
  278. return tagOpenAttributeValueQuoted
  279. }
  280. if (markdownLineEnding(code)) {
  281. returnState = tagOpenAttributeValueBefore
  282. return atLineEnding(code)
  283. }
  284. if (markdownSpace(code)) {
  285. effects.consume(code)
  286. return tagOpenAttributeValueBefore
  287. }
  288. effects.consume(code)
  289. marker = undefined
  290. return tagOpenAttributeValueUnquoted
  291. }
  292. function tagOpenAttributeValueQuoted(code) {
  293. if (code === marker) {
  294. effects.consume(code)
  295. return tagOpenAttributeValueQuotedAfter
  296. }
  297. if (code === codes.eof) {
  298. return nok(code)
  299. }
  300. if (markdownLineEnding(code)) {
  301. returnState = tagOpenAttributeValueQuoted
  302. return atLineEnding(code)
  303. }
  304. effects.consume(code)
  305. return tagOpenAttributeValueQuoted
  306. }
  307. function tagOpenAttributeValueQuotedAfter(code) {
  308. if (
  309. code === codes.greaterThan ||
  310. code === codes.slash ||
  311. markdownLineEndingOrSpace(code)
  312. ) {
  313. return tagOpenBetween(code)
  314. }
  315. return nok(code)
  316. }
  317. function tagOpenAttributeValueUnquoted(code) {
  318. if (
  319. code === codes.eof ||
  320. code === codes.quotationMark ||
  321. code === codes.apostrophe ||
  322. code === codes.lessThan ||
  323. code === codes.equalsTo ||
  324. code === codes.graveAccent
  325. ) {
  326. return nok(code)
  327. }
  328. if (code === codes.greaterThan || markdownLineEndingOrSpace(code)) {
  329. return tagOpenBetween(code)
  330. }
  331. effects.consume(code)
  332. return tagOpenAttributeValueUnquoted
  333. }
  334. // We can’t have blank lines in content, so no need to worry about empty
  335. // tokens.
  336. function atLineEnding(code) {
  337. assert(returnState, 'expected return state')
  338. assert(markdownLineEnding(code), 'expected eol')
  339. effects.exit(types.htmlTextData)
  340. effects.enter(types.lineEnding)
  341. effects.consume(code)
  342. effects.exit(types.lineEnding)
  343. return spaceFactory(
  344. effects,
  345. afterPrefix,
  346. types.linePrefix,
  347. self.parser.constructs.disable.null.indexOf('codeIndented') > -1
  348. ? undefined
  349. : constants.tabSize
  350. )
  351. }
  352. function afterPrefix(code) {
  353. effects.enter(types.htmlTextData)
  354. return returnState(code)
  355. }
  356. function end(code) {
  357. if (code === codes.greaterThan) {
  358. effects.consume(code)
  359. effects.exit(types.htmlTextData)
  360. effects.exit(types.htmlText)
  361. return ok
  362. }
  363. return nok(code)
  364. }
  365. }