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.

README.md 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # comment-parser
  2. `comment-parser` is a library helping to handle Generic JSDoc-style comments. It is
  3. - **language-agnostic** – no semantics enforced. You decide what tags are and what they mean. And it can be used with any language supporting `/** */` source comments.
  4. - **no dependencies** – it is compact and environment-agnostic, can be run on both the server and browser sides
  5. - **highly customizable** – with a little code you can deeply customize how comments are parsed
  6. - **bidirectional** - you can write comment blocks back to the source after updating or formatting
  7. - **strictly typed** - comes with generated `d.ts` data definitions since written in TypeScript
  8. ```sh
  9. npm install comment-parser
  10. ```
  11. > 💡 Check out the [Playground](https://syavorsky.github.io/comment-parser)
  12. > 💡 Previous version lives in [0.x](https://github.com/syavorsky/comment-parser/tree/0.x) branch
  13. Lib mainly provides two pieces [Parser](#Parser) and [Stringifier](#Stringifier).
  14. ## Parser
  15. Let's go over string parsing:
  16. ```js
  17. const { parse } = require('comment-parser/lib')
  18. const source = `
  19. /**
  20. * Description may go
  21. * over few lines followed by @tags
  22. * @param {string} name the name parameter
  23. * @param {any} value the value of any type
  24. */`
  25. const parsed = parse(source)
  26. ```
  27. Lib source code is written in TypeScript and all data shapes are conveniently available for your IDE of choice. All types described below can be found in [primitives.ts](src/primitives.ts)
  28. The input source is first parsed into lines, then lines split into tokens, and finally, tokens are processed into blocks of tags
  29. ### Block
  30. ```js
  31. /**
  32. * Description may go
  33. * over multiple lines followed by @tags
  34. * @param {string} name the name parameter
  35. * @param {any} value the value parameter
  36. */
  37. ```
  38. ### Description
  39. ```js
  40. /**
  41. * Description may go
  42. * over multiple lines followed by @tags
  43. ```
  44. ### Tags
  45. ```js
  46. * @param {string} name the name parameter
  47. ```
  48. ```js
  49. * @param {any} value the value parameter
  50. */
  51. ```
  52. ### Tokens
  53. ```
  54. |line|start|delimiter|postDelimiter|tag |postTag|name |postName|type |postType|description |end|
  55. |----|-----|---------|-------------|------|-------|-----|--------|--------|--------|--------------------------------|---|
  56. | 0|{2} |/** | | | | | | | | | |
  57. | 1|{3} |* |{1} | | | | | | |Description may go | |
  58. | 2|{3} |* |{1} | | | | | | |over few lines followed by @tags| |
  59. | 3|{3} |* |{1} |@param|{1} |name |{1} |{string}|{1} |the name parameter | |
  60. | 4|{3} |* |{1} |@param|{1} |value|{1} |{any} |{1} |the value of any type | |
  61. | 5|{3} | | | | | | | | | |*/ |
  62. ```
  63. ### Result
  64. The result is an array of Block objects, see the full output on the [playground](https://syavorsky.github.io/comment-parser)
  65. ```js
  66. [{
  67. // uppper text of the comment, overall block description
  68. description: 'Description may go over multiple lines followed by @tags',
  69. // list of block tags: @param, @param
  70. tags: [{
  71. // tokens.tag without "@"
  72. tag: 'param',
  73. // unwrapped tokens.name
  74. name: 'name',
  75. // unwrapped tokens.type
  76. type: 'string',
  77. // true, if tokens.name is [optional]
  78. optional: false,
  79. // default value if optional [name=default] has one
  80. default: undefined,
  81. // tokens.description assembled from a siongle or multiple lines
  82. description: 'the name parameter',
  83. // problems occured while parsing this tag section, subset of ../problems array
  84. problems: [],
  85. // source lines processed for extracting this tag, "slice" of the ../source item reference
  86. source: [ ... ],
  87. }, ... ],
  88. // source is an array of `Line` items having the source
  89. // line number and `Tokens` that can be assembled back into
  90. // the line string preserving original formatting
  91. source: [{
  92. // source line number
  93. number: 1,
  94. // source line string
  95. source: "/**",
  96. // source line tokens
  97. tokens: {
  98. // indentation
  99. start: "",
  100. // delimiter, either '/**', '*/', '*', or ''. Mid lines may have no delimiters
  101. delimiter: "/**",
  102. // space between delimiter and tag
  103. postDelimiter: "",
  104. // tag starting with "@"
  105. tag: "",
  106. // space between tag and type
  107. postTag: "",
  108. // name with no whitespaces or "multiple words" wrapped into quotes. May occure in [name] and [name=default] forms
  109. name: "",
  110. // space between name and type
  111. postName: "",
  112. // type is has to be {wrapped} into curlies otherwise will be omitted
  113. type: "",
  114. // space between type and description
  115. postType: "",
  116. // description is basicaly rest of the line
  117. description: "",
  118. // closing */ marker if present
  119. end: ""
  120. }
  121. }, ... ],
  122. // problems occured while parsing the block
  123. problems: [],
  124. }];
  125. ```
  126. While `.source[].tokens` are not providing readable annotation information, they are essential for tracing data origins and assembling string blocks with `stringify`
  127. ### options
  128. ```ts
  129. interface Options {
  130. // start count for source line numbers
  131. startLine: number;
  132. // escaping chars sequence marking wrapped content literal for the parser
  133. fence: string;
  134. // block and comment description compaction strategy
  135. spacing: 'compact' | 'preserve';
  136. // tokenizer functions extracting name, type, and description out of tag, see Tokenizer
  137. tokenizers: Tokenizer[];
  138. }
  139. ```
  140. examples
  141. - [default config](https://syavorsky.github.io/comment-parser/#parse-defaults)
  142. - [line numbers control](https://syavorsky.github.io/comment-parser/#parse-line-numbering)
  143. - [description spacing](https://syavorsky.github.io/comment-parser/#parse-spacing)
  144. - [escaping](https://syavorsky.github.io/comment-parser/#parse-escaping)
  145. - [explore the origin source](https://syavorsky.github.io/comment-parser/#parse-source-exploration)
  146. [suggest more examples](https://github.com/syavorsky/comment-parser/issues/new?title=example+suggestion%3A+...&labels=example,parser)
  147. ## Stringifier
  148. The stringifier is an important piece used by other tools updating the source code. It goes over `Block.source[].tokens` items and assembles them back to the string. It might be used with various transforms applied before stringifying.
  149. ```js
  150. const { parse, stringify, transforms: {flow, align, indent} } = require('comment-parser');
  151. const source = `
  152. /**
  153. * Description may go
  154. * over multiple lines followed by @tags
  155. *
  156. * @my-tag {my.type} my-name description line 1
  157. description line 2
  158. * description line 3
  159. */`;
  160. const parsed = parse(source);
  161. const transform = flow(align(), indent(0))
  162. console.log(stringify(transform(parsed[0])));
  163. ```
  164. ### Result
  165. ```js
  166. /**
  167. * Description may go
  168. * over multiple lines followed by @tags
  169. *
  170. * @my-tag {my.type} my-name description line 1
  171. description line 2
  172. * description line 3
  173. */
  174. ```
  175. examples
  176. - [format comments](https://syavorsky.github.io/comment-parser/#stringify-formatting)
  177. [suggest more examples](https://github.com/syavorsky/comment-parser/issues/new?title=example+suggestion%3A+...&labels=example,stringifier)
  178. ## Migrating from 0.x version
  179. Code of pre-1.0 version is forked into [0.x](https://github.com/syavorsky/comment-parser/tree/0.x) and will phase out eventually. Please file the issue if you find some previously existing functionality can't be achieved with 1.x API. Check out [migration notes](migrate-1.0.md).