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.js 4.8KB

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