preprocess.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Generate html files, target-dependent
  3. * Context for embedding page is loaded from markdown file
  4. */
  5. const process = require('process');
  6. const path = require('path');
  7. const fs = require('fs');
  8. const posthtml = require('posthtml');
  9. const expressions = require('posthtml-expressions');
  10. const marked = require('marked');
  11. const config = require('../build.json');
  12. const options = require('../src/js/options.json');
  13. const target = process.env.NODE_ENV || 'development';
  14. // Change current directory to script location so that
  15. // the relative paths of the resources can be resolved
  16. process.chdir(path.dirname(process.argv[1]));
  17. function createOutputDirectory() {
  18. fs.mkdirSync(
  19. path.join(
  20. '../',
  21. config.path.output,
  22. config.path.data
  23. ),
  24. { recursive: true },
  25. (error) => {
  26. if (error !== null) {
  27. console.error('Error while creating directory', error);
  28. }
  29. }
  30. );
  31. }
  32. // Load info from markdown file
  33. function loadFile(filepath) {
  34. const content = fs.readFileSync(filepath, 'utf8');
  35. return content;
  36. }
  37. function createHtml() {
  38. // Create visualisation HTML
  39. const visualisation = loadFile(path.join(
  40. '../',
  41. config.path.input,
  42. config.path.html,
  43. config.html.input
  44. ));
  45. posthtml(
  46. expressions({
  47. locals: {
  48. javascript: path.join(config.path.js, `${config.main}${config[target].infix}.js`),
  49. stylesheet: path.join(config.path.css, `${config.main}${config[target].infix}.css`)
  50. }
  51. })
  52. ).process(visualisation)
  53. .then((result) => {
  54. fs.writeFileSync(
  55. path.join(
  56. '../',
  57. config.path.output,
  58. // config.path.html,
  59. config.html.output
  60. ), result.html
  61. );
  62. });
  63. }
  64. function createInfoComponent(md) {
  65. var converted = `<sampling-info>${marked(md)}</sampling-info>`;
  66. // Compile html, write file
  67. posthtml().process(converted)
  68. .then((result) => {
  69. fs.writeFileSync(
  70. path.join(
  71. '../',
  72. config.path.input,
  73. config.path.js,
  74. config.path.components,
  75. config.info.output.component
  76. ), result.html
  77. );
  78. });
  79. }
  80. // Convert markdown to json for use in pdf export using marked
  81. // https://marked.js.org/#/USING_PRO.md#tokenizer
  82. // So far, only headings, paragraphs and lists are supported
  83. function createInfoJSON(md) {
  84. var tokens = marked.lexer(md);
  85. let info = [];
  86. for (let token of tokens) {
  87. if (token.type === 'space') {
  88. continue;
  89. }
  90. if (token.type === 'html') {
  91. info.push({
  92. text: '',
  93. type: 'pageBreak'
  94. });
  95. } else { // if (token.type === 'list' || token.type === 'heading' || token.type === 'paragraph') {
  96. var object = {
  97. type: token.type
  98. };
  99. if (token.type === 'list') {
  100. object.items = token.items.map(i => i.text);
  101. } else {
  102. object.text = token.text;
  103. }
  104. if (token.type === 'heading') {
  105. object.depth = token.depth;
  106. }
  107. info.push(object);
  108. }
  109. }
  110. fs.writeFileSync(
  111. path.join(
  112. '../',
  113. config.path.output,
  114. config.path.data,
  115. config.info.output.javascript
  116. ), JSON.stringify(info)
  117. );
  118. }
  119. function createInfo () {
  120. // Convert markdown to html
  121. var markdown = loadFile(
  122. path.join(
  123. '../',
  124. config.path.data,
  125. config.info.input
  126. )
  127. );
  128. createInfoComponent(markdown);
  129. createInfoJSON(markdown);
  130. }
  131. createOutputDirectory();
  132. createInfo();
  133. createHtml();