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.

block-parser.ts 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Line } from '../primitives';
  2. const reTag = /^@\S+/;
  3. /**
  4. * Groups source lines in sections representing tags.
  5. * First section is a block description if present. Last section captures lines starting with
  6. * the last tag to the end of the block, including dangling closing marker.
  7. * @param {Line[]} block souce lines making a single comment block
  8. */
  9. export type Parser = (block: Line[]) => Line[][];
  10. /**
  11. * Predicate telling if string contains opening/closing escaping sequence
  12. * @param {string} source raw source line
  13. */
  14. export type Fencer = (source: string) => boolean;
  15. /**
  16. * `Parser` configuration options
  17. */
  18. export interface Options {
  19. // escaping sequence or predicate
  20. fence: string | Fencer;
  21. }
  22. /**
  23. * Creates configured `Parser`
  24. * @param {Partial<Options>} options
  25. */
  26. export default function getParser({
  27. fence = '```',
  28. }: Partial<Options> = {}): Parser {
  29. const fencer = getFencer(fence);
  30. const toggleFence = (source: string, isFenced: boolean): boolean =>
  31. fencer(source) ? !isFenced : isFenced;
  32. return function parseBlock(source: Line[]): Line[][] {
  33. // start with description section
  34. const sections: Line[][] = [[]];
  35. let isFenced = false;
  36. for (const line of source) {
  37. if (reTag.test(line.tokens.description) && !isFenced) {
  38. sections.push([line]);
  39. } else {
  40. sections[sections.length - 1].push(line);
  41. }
  42. isFenced = toggleFence(line.tokens.description, isFenced);
  43. }
  44. return sections;
  45. };
  46. }
  47. function getFencer(fence: string | Fencer): Fencer {
  48. if (typeof fence === 'string')
  49. return (source: string) => source.split(fence).length % 2 === 0;
  50. return fence;
  51. }