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.

text.mjs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. export var text = initializeFactory('text')
  2. export var string = initializeFactory('string')
  3. export var resolver = {resolveAll: createResolver()}
  4. import codes from '../character/codes.mjs'
  5. import assign from '../constant/assign.mjs'
  6. import constants from '../constant/constants.mjs'
  7. import types from '../constant/types.mjs'
  8. import shallow from '../util/shallow.mjs'
  9. function initializeFactory(field) {
  10. return {
  11. tokenize: initializeText,
  12. resolveAll: createResolver(
  13. field === 'text' ? resolveAllLineSuffixes : undefined
  14. )
  15. }
  16. function initializeText(effects) {
  17. var self = this
  18. var constructs = this.parser.constructs[field]
  19. var text = effects.attempt(constructs, start, notText)
  20. return start
  21. function start(code) {
  22. return atBreak(code) ? text(code) : notText(code)
  23. }
  24. function notText(code) {
  25. if (code === codes.eof) {
  26. effects.consume(code)
  27. return
  28. }
  29. effects.enter(types.data)
  30. effects.consume(code)
  31. return data
  32. }
  33. function data(code) {
  34. if (atBreak(code)) {
  35. effects.exit(types.data)
  36. return text(code)
  37. }
  38. // Data.
  39. effects.consume(code)
  40. return data
  41. }
  42. function atBreak(code) {
  43. var list = constructs[code]
  44. var index = -1
  45. if (code === codes.eof) {
  46. return true
  47. }
  48. if (list) {
  49. while (++index < list.length) {
  50. if (
  51. !list[index].previous ||
  52. list[index].previous.call(self, self.previous)
  53. ) {
  54. return true
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. function createResolver(extraResolver) {
  62. return resolveAllText
  63. function resolveAllText(events, context) {
  64. var index = -1
  65. var enter
  66. // A rather boring computation (to merge adjacent `data` events) which
  67. // improves mm performance by 29%.
  68. while (++index <= events.length) {
  69. if (enter === undefined) {
  70. if (events[index] && events[index][1].type === types.data) {
  71. enter = index
  72. index++
  73. }
  74. } else if (!events[index] || events[index][1].type !== types.data) {
  75. // Don’t do anything if there is one data token.
  76. if (index !== enter + 2) {
  77. events[enter][1].end = events[index - 1][1].end
  78. events.splice(enter + 2, index - enter - 2)
  79. index = enter + 2
  80. }
  81. enter = undefined
  82. }
  83. }
  84. return extraResolver ? extraResolver(events, context) : events
  85. }
  86. }
  87. // A rather ugly set of instructions which again looks at chunks in the input
  88. // stream.
  89. // The reason to do this here is that it is *much* faster to parse in reverse.
  90. // And that we can’t hook into `null` to split the line suffix before an EOF.
  91. // To do: figure out if we can make this into a clean utility, or even in core.
  92. // As it will be useful for GFMs literal autolink extension (and maybe even
  93. // tables?)
  94. function resolveAllLineSuffixes(events, context) {
  95. var eventIndex = -1
  96. var chunks
  97. var data
  98. var chunk
  99. var index
  100. var bufferIndex
  101. var size
  102. var tabs
  103. var token
  104. while (++eventIndex <= events.length) {
  105. if (
  106. (eventIndex === events.length ||
  107. events[eventIndex][1].type === types.lineEnding) &&
  108. events[eventIndex - 1][1].type === types.data
  109. ) {
  110. data = events[eventIndex - 1][1]
  111. chunks = context.sliceStream(data)
  112. index = chunks.length
  113. bufferIndex = -1
  114. size = 0
  115. tabs = undefined
  116. while (index--) {
  117. chunk = chunks[index]
  118. if (typeof chunk === 'string') {
  119. bufferIndex = chunk.length
  120. while (chunk.charCodeAt(bufferIndex - 1) === codes.space) {
  121. size++
  122. bufferIndex--
  123. }
  124. if (bufferIndex) break
  125. bufferIndex = -1
  126. }
  127. // Number
  128. else if (chunk === codes.horizontalTab) {
  129. tabs = true
  130. size++
  131. } else if (chunk === codes.virtualSpace) {
  132. // Empty
  133. } else {
  134. // Replacement character, exit.
  135. index++
  136. break
  137. }
  138. }
  139. if (size) {
  140. token = {
  141. type:
  142. eventIndex === events.length ||
  143. tabs ||
  144. size < constants.hardBreakPrefixSizeMin
  145. ? types.lineSuffix
  146. : types.hardBreakTrailing,
  147. start: {
  148. line: data.end.line,
  149. column: data.end.column - size,
  150. offset: data.end.offset - size,
  151. _index: data.start._index + index,
  152. _bufferIndex: index
  153. ? bufferIndex
  154. : data.start._bufferIndex + bufferIndex
  155. },
  156. end: shallow(data.end)
  157. }
  158. data.end = shallow(token.start)
  159. if (data.start.offset === data.end.offset) {
  160. assign(data, token)
  161. } else {
  162. events.splice(
  163. eventIndex,
  164. 0,
  165. ['enter', token, context],
  166. ['exit', token, context]
  167. )
  168. eventIndex += 2
  169. }
  170. }
  171. eventIndex++
  172. }
  173. }
  174. return events
  175. }