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.

examples.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // This file is a source for playground examples.
  2. // Examples integrity is smoke-checked with examples.spec.js
  3. function parse_defaults(source, parse, stringify, transforms) {
  4. // Invoking parser with default config covers the most genearic cases.
  5. // Note how /*** and /* blocks are ignored
  6. /** One-liner */
  7. /** @some-tag {someType} someName */
  8. /**
  9. * Description may go
  10. * over multiple lines followed by @tags
  11. * @param {string} name the name parameter
  12. * @param {any} value the value parameter
  13. */
  14. /*** ignored */
  15. /* ignored */
  16. const parsed = parse(source);
  17. const stringified = parsed.map((block) => stringify(block));
  18. }
  19. function parse_line_numbering(source, parse, stringify, transforms) {
  20. // Note, line numbers are off by 5 from what you see in editor
  21. //
  22. // Try changeing start line back to 0, or omit the option
  23. // parse(source, {startLine: 0}) -- default
  24. // parse(source, {startLine: 5}) -- enforce alternative start number
  25. /**
  26. * Description may go
  27. * over multiple lines followed by @tags
  28. * @param {string} name the name parameter
  29. * @param {any} value the value parameter
  30. */
  31. const parsed = parse(source, { startLine: 5 });
  32. const stringified = parsed[0].tags
  33. .map((tag) => `line ${tag.source[0].number + 1} : @${tag.tag} ${tag.name}`)
  34. .join('\n');
  35. }
  36. function parse_spacing(source, parse, stringify, transforms) {
  37. // Note, when spacing option is set to 'compact' or omited, tag and block descriptions are collapsed to look like a single sentense.
  38. //
  39. // Try changeing it to 'preserve' or defining custom function
  40. // parse(source, {spacing: 'compact'}) -- default
  41. // parse(source, {spacing: 'preserve'}) -- preserve spaces and line breaks
  42. // parse(source, {spacing: lines => lines
  43. // .map(tokens => tokens.description.trim())
  44. // .filter(description => description !== '')
  45. // .join(' ');
  46. // }) -- mimic 'compact' implementation
  47. /**
  48. * Description may go
  49. * over multiple lines followed by
  50. * @param {string} name the name parameter
  51. * with multiline description
  52. * @param {function(
  53. * number,
  54. * string
  55. * )} options the options
  56. */
  57. const parsed = parse(source, { spacing: 'preserve' });
  58. const stringified = parsed[0].tags
  59. .map((tag) => `@${tag.tag} - ${tag.description}\n\n${tag.type}`)
  60. .join('\n----\n');
  61. }
  62. function parse_escaping(source, parse, stringify, transforms) {
  63. // Note, @decorator is not parsed as another tag because block is wrapped into ###.
  64. //
  65. // Try setting alternative escape sequence
  66. // parse(source, {fence: '```'}) -- default
  67. // parse(source, {fence: '###'}) -- update source correspondingly
  68. /**
  69. * @example "some code"
  70. ###
  71. @decorator
  72. function hello() {
  73. // do something
  74. }
  75. ###
  76. */
  77. const parsed = parse(source, { fence: '###' });
  78. const stringified = parsed[0].tags
  79. .map((tag) => `@${tag.tag} - ${tag.description}`)
  80. .join('\n');
  81. }
  82. function stringify_formatting(source, parse, stringify, transforms) {
  83. // stringify preserves exact formatting by default, but you can transform parsing result first
  84. // transform = align() -- align name, type, and description
  85. // transform = flow(align(), indent(4)) -- align, then place the block's opening marker at pos 4
  86. /**
  87. * Description may go
  88. * over multiple lines followed by @tags
  89. * @param {string} name the name parameter
  90. * @param {any} value the value parameter
  91. */
  92. const { flow, align, indent } = transforms;
  93. const transform = flow(align(), indent(4));
  94. const parsed = parse(source);
  95. const stringified = stringify(transform(parsed[0]));
  96. }
  97. function parse_source_exploration(source, parse, stringify, transforms) {
  98. // parse() produces Block[].source keeping accurate track of origin source
  99. /**
  100. * Description may go
  101. * over multiple lines followed by @tags
  102. * @param {string} name the name parameter
  103. * @param {any} value the value parameter
  104. */
  105. const parsed = parse(source);
  106. const summary = ({ source }) => ({
  107. source: source
  108. .map(
  109. ({ tokens }) =>
  110. tokens.start +
  111. tokens.delimiter +
  112. tokens.postDelimiter +
  113. tokens.tag +
  114. tokens.postTag +
  115. tokens.type +
  116. tokens.postType +
  117. tokens.name +
  118. tokens.postName +
  119. tokens.description +
  120. tokens.end
  121. )
  122. .join('\n'),
  123. start: {
  124. line: source[0].number + 1,
  125. column: source[0].tokens.start.length,
  126. },
  127. end: {
  128. line: source[source.length - 1].number + 1,
  129. column: source[source.length - 1].source.length,
  130. },
  131. });
  132. const pos = (p) => p.line + ':' + p.column;
  133. const stringified = parsed[0].tags
  134. .map(summary)
  135. .map((s) => `${pos(s.start)} - ${pos(s.end)}\n${s.source}`);
  136. }
  137. function parse_advanced_parsing(source, parse, _, _, tokenizers) {
  138. // Each '@tag ...' section results into a Spec. The Spec is computed by
  139. // the chain of tokenizers each contributing a change to the the Spec.* and the Spec.tags[].tokens.
  140. // Default parse() options come with stadart tokenizers:
  141. // {
  142. // ...,
  143. // spacing = 'compact',
  144. // tokenizers = [
  145. // tokenizers.tag(),
  146. // tokenizers.type(spacing),
  147. // tokenizers.name(),
  148. // tokenizers.description(spacing),
  149. // ]
  150. // }
  151. // You can reorder those, or even replace any with a custom function (spec: Spec) => Spec
  152. // This example allows to parse "@tag description" comments
  153. /**
  154. * @arg0 my parameter
  155. * @arg1
  156. * another parameter
  157. * with a strange formatting
  158. */
  159. const parsed = parse(source, {
  160. tokenizers: [tokenizers.tag(), tokenizers.description('preserve')],
  161. });
  162. const stringified = parsed[0].tags
  163. .map((tag) => `@${tag.tag} - ${tag.description}`)
  164. .join('\n');
  165. }
  166. (typeof window === 'undefined' ? module.exports : window).examples = [
  167. parse_defaults,
  168. parse_line_numbering,
  169. parse_escaping,
  170. parse_spacing,
  171. parse_source_exploration,
  172. parse_advanced_parsing,
  173. stringify_formatting,
  174. ];