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.

135 lines
3.4 KiB

  1. fs = require('fs')
  2. function md_trim(str, context) {
  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. var entry_count = 0;
  38. function read_line() {
  39. return lines[cur_line++];
  40. }
  41. var root = {};
  42. while (true) {
  43. var cols = [];
  44. var rows = [];
  45. while (line.indexOf(anchor) == -1 && cur_line != lines.length) {
  46. line = read_line();
  47. }
  48. if (cur_line == lines.length) {
  49. break;
  50. }
  51. table_name = line.split(anchor)[1];
  52. table_name = md_trim(table_name, 0)
  53. line = read_line()
  54. if (line) {
  55. line = line.split("|")
  56. for (var j in line) {
  57. line[j] = md_trim(line[j], 0)
  58. if ((j == 0 || j == line.length - 1) && line[j] === "") {
  59. } else {
  60. cols.push(line[j]);
  61. }
  62. }
  63. if (line.length) {
  64. cols = line;
  65. rows.push(cols)
  66. } else {
  67. console.error("markdown expect column title")
  68. break;
  69. }
  70. } else {
  71. console.error("markdown expect table content")
  72. break;
  73. }
  74. line = read_line()
  75. if (!line) {
  76. console.error("markdown expect table spliter")
  77. break;
  78. }
  79. line = read_line()
  80. while (line.indexOf("|") != -1 && cur_line != lines.length) {
  81. var line_this = line.split("|")
  82. var row = []
  83. for (var j in line_this) {
  84. line_this[j] = md_trim(line_this[j], j)
  85. if ((j == 0 || j == line_this.length - 1) && line_this[j] === "") {
  86. } else {
  87. row.push(line_this[j]);
  88. }
  89. }
  90. rows.push(row);
  91. entry_count++;
  92. line = read_line()
  93. }
  94. var data=[];
  95. for (var j in rows) {
  96. if (j != 0) {
  97. var ele = {};
  98. for (var k in rows[j]) {
  99. ele[rows[0][k]] = rows[j][k];
  100. }
  101. data.push(ele);
  102. }
  103. }
  104. root["count"] = entry_count;
  105. root[table_name] = data;
  106. }
  107. console.log(JSON.stringify(root));
  108. });
  109. }
  110. if (process.argv.length < 3) {
  111. console.log("No .md file passed!");
  112. return;
  113. }
  114. if (process.argv.length < 4) {
  115. anchorText = "###";
  116. } else {
  117. anchorText = process.argv[3];
  118. }
  119. handle(process.argv[2].toString(), anchorText);