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.

attention.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. 'use strict'
  2. var assert = require('assert')
  3. var codes = require('../character/codes.js')
  4. var constants = require('../constant/constants.js')
  5. var types = require('../constant/types.js')
  6. var chunkedPush = require('../util/chunked-push.js')
  7. var chunkedSplice = require('../util/chunked-splice.js')
  8. var classifyCharacter = require('../util/classify-character.js')
  9. var movePoint = require('../util/move-point.js')
  10. var resolveAll = require('../util/resolve-all.js')
  11. var shallow = require('../util/shallow.js')
  12. function _interopDefaultLegacy(e) {
  13. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  14. }
  15. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  16. var attention = {
  17. name: 'attention',
  18. tokenize: tokenizeAttention,
  19. resolveAll: resolveAllAttention
  20. }
  21. // Take all events and resolve attention to emphasis or strong.
  22. function resolveAllAttention(events, context) {
  23. var index = -1
  24. var open
  25. var group
  26. var text
  27. var openingSequence
  28. var closingSequence
  29. var use
  30. var nextEvents
  31. var offset
  32. // Walk through all events.
  33. //
  34. // Note: performance of this is fine on an mb of normal markdown, but it’s
  35. // a bottleneck for malicious stuff.
  36. while (++index < events.length) {
  37. // Find a token that can close.
  38. if (
  39. events[index][0] === 'enter' &&
  40. events[index][1].type === 'attentionSequence' &&
  41. events[index][1]._close
  42. ) {
  43. open = index
  44. // Now walk back to find an opener.
  45. while (open--) {
  46. // Find a token that can open the closer.
  47. if (
  48. events[open][0] === 'exit' &&
  49. events[open][1].type === 'attentionSequence' &&
  50. events[open][1]._open &&
  51. // If the markers are the same:
  52. context.sliceSerialize(events[open][1]).charCodeAt(0) ===
  53. context.sliceSerialize(events[index][1]).charCodeAt(0)
  54. ) {
  55. // If the opening can close or the closing can open,
  56. // and the close size *is not* a multiple of three,
  57. // but the sum of the opening and closing size *is* multiple of three,
  58. // then don’t match.
  59. if (
  60. (events[open][1]._close || events[index][1]._open) &&
  61. (events[index][1].end.offset - events[index][1].start.offset) % 3 &&
  62. !(
  63. (events[open][1].end.offset -
  64. events[open][1].start.offset +
  65. events[index][1].end.offset -
  66. events[index][1].start.offset) %
  67. 3
  68. )
  69. ) {
  70. continue
  71. }
  72. // Number of markers to use from the sequence.
  73. use =
  74. events[open][1].end.offset - events[open][1].start.offset > 1 &&
  75. events[index][1].end.offset - events[index][1].start.offset > 1
  76. ? 2
  77. : 1
  78. openingSequence = {
  79. type: use > 1 ? types.strongSequence : types.emphasisSequence,
  80. start: movePoint(shallow(events[open][1].end), -use),
  81. end: shallow(events[open][1].end)
  82. }
  83. closingSequence = {
  84. type: use > 1 ? types.strongSequence : types.emphasisSequence,
  85. start: shallow(events[index][1].start),
  86. end: movePoint(shallow(events[index][1].start), use)
  87. }
  88. text = {
  89. type: use > 1 ? types.strongText : types.emphasisText,
  90. start: shallow(events[open][1].end),
  91. end: shallow(events[index][1].start)
  92. }
  93. group = {
  94. type: use > 1 ? types.strong : types.emphasis,
  95. start: shallow(openingSequence.start),
  96. end: shallow(closingSequence.end)
  97. }
  98. events[open][1].end = shallow(openingSequence.start)
  99. events[index][1].start = shallow(closingSequence.end)
  100. nextEvents = []
  101. // If there are more markers in the opening, add them before.
  102. if (events[open][1].end.offset - events[open][1].start.offset) {
  103. nextEvents = chunkedPush(nextEvents, [
  104. ['enter', events[open][1], context],
  105. ['exit', events[open][1], context]
  106. ])
  107. }
  108. // Opening.
  109. nextEvents = chunkedPush(nextEvents, [
  110. ['enter', group, context],
  111. ['enter', openingSequence, context],
  112. ['exit', openingSequence, context],
  113. ['enter', text, context]
  114. ])
  115. // Between.
  116. nextEvents = chunkedPush(
  117. nextEvents,
  118. resolveAll(
  119. context.parser.constructs.insideSpan.null,
  120. events.slice(open + 1, index),
  121. context
  122. )
  123. )
  124. // Closing.
  125. nextEvents = chunkedPush(nextEvents, [
  126. ['exit', text, context],
  127. ['enter', closingSequence, context],
  128. ['exit', closingSequence, context],
  129. ['exit', group, context]
  130. ])
  131. // If there are more markers in the closing, add them after.
  132. if (events[index][1].end.offset - events[index][1].start.offset) {
  133. offset = 2
  134. nextEvents = chunkedPush(nextEvents, [
  135. ['enter', events[index][1], context],
  136. ['exit', events[index][1], context]
  137. ])
  138. } else {
  139. offset = 0
  140. }
  141. chunkedSplice(events, open - 1, index - open + 3, nextEvents)
  142. index = open + nextEvents.length - offset - 2
  143. break
  144. }
  145. }
  146. }
  147. }
  148. // Remove remaining sequences.
  149. index = -1
  150. while (++index < events.length) {
  151. if (events[index][1].type === 'attentionSequence') {
  152. events[index][1].type = 'data'
  153. }
  154. }
  155. return events
  156. }
  157. function tokenizeAttention(effects, ok) {
  158. var before = classifyCharacter(this.previous)
  159. var marker
  160. return start
  161. function start(code) {
  162. assert__default['default'](
  163. code === codes.asterisk || code === codes.underscore,
  164. 'expected asterisk or underscore'
  165. )
  166. effects.enter('attentionSequence')
  167. marker = code
  168. return sequence(code)
  169. }
  170. function sequence(code) {
  171. var token
  172. var after
  173. var open
  174. var close
  175. if (code === marker) {
  176. effects.consume(code)
  177. return sequence
  178. }
  179. token = effects.exit('attentionSequence')
  180. after = classifyCharacter(code)
  181. open = !after || (after === constants.characterGroupPunctuation && before)
  182. close = !before || (before === constants.characterGroupPunctuation && after)
  183. token._open = marker === codes.asterisk ? open : open && (before || !close)
  184. token._close = marker === codes.asterisk ? close : close && (after || !open)
  185. return ok(code)
  186. }
  187. }
  188. module.exports = attention