No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

113 líneas
2.7 KiB

  1. fs = require('fs')
  2. function md_trim(str) {
  3. return str.replace(/(^\s+)|(\s+$)/g, "");
  4. }
  5. function handle(filename, anchor) {
  6. fs.readFile(filename, 'utf8', function (err,text) {
  7. if (err) {
  8. return console.log(err);
  9. }
  10. var lines = text.split("\n");
  11. var cur_line = 0;
  12. var line = ""
  13. var table_name = "";
  14. var col_num = 0;
  15. var cols = [];
  16. var rows = [];
  17. function read_line() {
  18. return lines[cur_line++];
  19. }
  20. var root = {};
  21. while (true) {
  22. var cols = [];
  23. var rows = [];
  24. while (line.indexOf(anchor) == -1 && cur_line != lines.length) {
  25. line = read_line();
  26. }
  27. if (cur_line == lines.length) {
  28. break;
  29. }
  30. table_name = line.split(anchor)[1];
  31. table_name = md_trim(table_name)
  32. line = read_line()
  33. if (line) {
  34. line = line.split("|")
  35. for (var j in line) {
  36. line[j] = md_trim(line[j])
  37. if ((j == 0 || j == line.length - 1) && line[j] === "") {
  38. } else {
  39. cols.push(line[j]);
  40. }
  41. }
  42. if (line.length) {
  43. cols = line;
  44. rows.push(cols)
  45. } else {
  46. console.error("markdown expect column title")
  47. break;
  48. }
  49. } else {
  50. console.error("markdown expect table content")
  51. break;
  52. }
  53. line = read_line()
  54. if (line) {
  55. } else {
  56. console.error("markdown expect table spliter")
  57. break;
  58. }
  59. line = read_line()
  60. while (line.indexOf("|") != -1 && cur_line != lines.length) {
  61. var line_this = line.split("|")
  62. var row = []
  63. for (var j in line_this) {
  64. line_this[j] = md_trim(line_this[j])
  65. if ((j == 0 || j == line_this.length - 1) && line_this[j] === "") {
  66. } else {
  67. row.push(line_this[j]);
  68. }
  69. }
  70. rows.push(row);
  71. line = read_line()
  72. }
  73. var data=[];
  74. for (var j in rows) {
  75. if (j != 0) {
  76. var ele = {};
  77. for (var k in rows[j]) {
  78. ele[rows[0][k]] = rows[j][k];
  79. }
  80. data.push(ele);
  81. }
  82. }
  83. root[table_name] = data;
  84. }
  85. console.log(JSON.stringify(root));
  86. });
  87. }
  88. if (process.argv.length < 3) {
  89. console.log("No .md file passed!");
  90. return;
  91. }
  92. if (process.argv.length < 4) {
  93. anchorText = "###";
  94. } else {
  95. anchorText = process.argv[3];
  96. }
  97. handle(process.argv[2].toString(), anchorText);