西藏巴青项目

extract-gis-paragraphs.mjs 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import fs from 'fs'
  2. import path from 'path'
  3. import os from 'os'
  4. const x = fs.readFileSync(path.join(os.tmpdir(), 'gis-doc-raw.xml'), 'utf8')
  5. function decodeEntities(s) {
  6. return s
  7. .replace(/&lt;/g, '<')
  8. .replace(/&gt;/g, '>')
  9. .replace(/&quot;/g, '"')
  10. .replace(/&amp;/g, '&')
  11. }
  12. function stripXml(chunk) {
  13. return decodeEntities(chunk)
  14. .replace(/<w:tab[^>]*\/>/g, '\t')
  15. .replace(/<\/w:p>/g, '\n')
  16. .replace(/<[^>]+>/g, '')
  17. .replace(/\n{3,}/g, '\n\n')
  18. .trim()
  19. }
  20. // Collect paragraphs that look like doc body (skip TOC hyperlinks)
  21. const paras = []
  22. for (const m of x.matchAll(/<w:p[\s\S]*?<\/w:p>/g)) {
  23. const t = stripXml(m[0])
  24. if (!t || t.length < 4) continue
  25. if (t.includes('PAGEREF') || t.includes('HYPERLINK \\l')) continue
  26. if (t.startsWith('TOC ')) continue
  27. paras.push(t)
  28. }
  29. const out = []
  30. const keywords = /GIS|cmmap|Map|token|密钥|Marker|GeoJSON|图层|地图|script|container|accessToken|NavigationControl|MouseTool|路线|POI/i
  31. let inSection = false
  32. for (let i = 0; i < paras.length; i++) {
  33. const p = paras[i]
  34. if (/^2[\.\s]/.test(p) || /^[0-9]+\s/.test(p) || keywords.test(p)) {
  35. inSection = true
  36. }
  37. if (inSection && keywords.test(p)) {
  38. out.push(p)
  39. // include next few lines if code-like
  40. for (let j = 1; j <= 3 && i + j < paras.length; j++) {
  41. const n = paras[i + j]
  42. if (n.includes('{') || n.includes('cmmap') || n.includes('map.') || n.includes('<')) {
  43. out.push(n)
  44. }
  45. }
  46. }
  47. }
  48. const unique = [...new Set(out)]
  49. const target = path.resolve('doc/大屏/集中化GIS系统JSAPI接口文档-提取.txt')
  50. fs.writeFileSync(target, unique.join('\n\n'), 'utf8')
  51. console.log('written', target, 'lines', unique.length)
  52. // Also dump section 2.x headings
  53. const headings = paras.filter((p) => /^[0-9]+(\.[0-9]+)*\s/.test(p) && p.length < 80)
  54. fs.writeFileSync(path.resolve('doc/大屏/集中化GIS系统JSAPI-目录.txt'), headings.join('\n'), 'utf8')
  55. console.log('headings', headings.length)