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.
 
 

132 line
3.3 KiB

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