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 5.8KB

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