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.

code-text.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. 'use strict'
  2. var assert = require('assert')
  3. var codes = require('../character/codes.js')
  4. var markdownLineEnding = require('../character/markdown-line-ending.js')
  5. var types = require('../constant/types.js')
  6. function _interopDefaultLegacy(e) {
  7. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  8. }
  9. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  10. var codeText = {
  11. name: 'codeText',
  12. tokenize: tokenizeCodeText,
  13. resolve: resolveCodeText,
  14. previous: previous
  15. }
  16. function resolveCodeText(events) {
  17. var tailExitIndex = events.length - 4
  18. var headEnterIndex = 3
  19. var index
  20. var enter
  21. // If we start and end with an EOL or a space.
  22. if (
  23. (events[headEnterIndex][1].type === types.lineEnding ||
  24. events[headEnterIndex][1].type === 'space') &&
  25. (events[tailExitIndex][1].type === types.lineEnding ||
  26. events[tailExitIndex][1].type === 'space')
  27. ) {
  28. index = headEnterIndex
  29. // And we have data.
  30. while (++index < tailExitIndex) {
  31. if (events[index][1].type === types.codeTextData) {
  32. // Then we have padding.
  33. events[tailExitIndex][1].type = events[headEnterIndex][1].type =
  34. types.codeTextPadding
  35. headEnterIndex += 2
  36. tailExitIndex -= 2
  37. break
  38. }
  39. }
  40. }
  41. // Merge adjacent spaces and data.
  42. index = headEnterIndex - 1
  43. tailExitIndex++
  44. while (++index <= tailExitIndex) {
  45. if (enter === undefined) {
  46. if (
  47. index !== tailExitIndex &&
  48. events[index][1].type !== types.lineEnding
  49. ) {
  50. enter = index
  51. }
  52. } else if (
  53. index === tailExitIndex ||
  54. events[index][1].type === types.lineEnding
  55. ) {
  56. events[enter][1].type = types.codeTextData
  57. if (index !== enter + 2) {
  58. events[enter][1].end = events[index - 1][1].end
  59. events.splice(enter + 2, index - enter - 2)
  60. tailExitIndex -= index - enter - 2
  61. index = enter + 2
  62. }
  63. enter = undefined
  64. }
  65. }
  66. return events
  67. }
  68. function previous(code) {
  69. // If there is a previous code, there will always be a tail.
  70. return (
  71. code !== codes.graveAccent ||
  72. this.events[this.events.length - 1][1].type === types.characterEscape
  73. )
  74. }
  75. function tokenizeCodeText(effects, ok, nok) {
  76. var self = this
  77. var sizeOpen = 0
  78. var size
  79. var token
  80. return start
  81. function start(code) {
  82. assert__default['default'](code === codes.graveAccent, 'expected `` ` ``')
  83. assert__default['default'](
  84. previous.call(self, self.previous),
  85. 'expected correct previous'
  86. )
  87. effects.enter(types.codeText)
  88. effects.enter(types.codeTextSequence)
  89. return openingSequence(code)
  90. }
  91. function openingSequence(code) {
  92. if (code === codes.graveAccent) {
  93. effects.consume(code)
  94. sizeOpen++
  95. return openingSequence
  96. }
  97. effects.exit(types.codeTextSequence)
  98. return gap(code)
  99. }
  100. function gap(code) {
  101. // EOF.
  102. if (code === codes.eof) {
  103. return nok(code)
  104. }
  105. // Closing fence?
  106. // Could also be data.
  107. if (code === codes.graveAccent) {
  108. token = effects.enter(types.codeTextSequence)
  109. size = 0
  110. return closingSequence(code)
  111. }
  112. // Tabs don’t work, and virtual spaces don’t make sense.
  113. if (code === codes.space) {
  114. effects.enter('space')
  115. effects.consume(code)
  116. effects.exit('space')
  117. return gap
  118. }
  119. if (markdownLineEnding(code)) {
  120. effects.enter(types.lineEnding)
  121. effects.consume(code)
  122. effects.exit(types.lineEnding)
  123. return gap
  124. }
  125. // Data.
  126. effects.enter(types.codeTextData)
  127. return data(code)
  128. }
  129. // In code.
  130. function data(code) {
  131. if (
  132. code === codes.eof ||
  133. code === codes.space ||
  134. code === codes.graveAccent ||
  135. markdownLineEnding(code)
  136. ) {
  137. effects.exit(types.codeTextData)
  138. return gap(code)
  139. }
  140. effects.consume(code)
  141. return data
  142. }
  143. // Closing fence.
  144. function closingSequence(code) {
  145. // More.
  146. if (code === codes.graveAccent) {
  147. effects.consume(code)
  148. size++
  149. return closingSequence
  150. }
  151. // Done!
  152. if (size === sizeOpen) {
  153. effects.exit(types.codeTextSequence)
  154. effects.exit(types.codeText)
  155. return ok(code)
  156. }
  157. // More or less accents: mark as data.
  158. token.type = types.codeTextData
  159. return data(code)
  160. }
  161. }
  162. module.exports = codeText