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

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