import fs from 'fs'
import path from 'path'
import os from 'os'

const x = fs.readFileSync(path.join(os.tmpdir(), 'gis-doc-raw.xml'), 'utf8')

function strip(chunk) {
  return chunk
    .replace(/<w:tab[^>]*\/>/g, '\t')
    .replace(/<\/w:p>/g, '\n')
    .replace(/<[^>]+>/g, '')
}

const terms = [
  'cmmapgl.accessToken',
  'setAccessToken',
  '地图密钥',
  '2.3 地图组件',
  'new cmmapgl.Map',
  'NavigationControl',
  'GeoJSON',
  '准备步骤',
  '基本准备'
]

for (const t of terms) {
  let idx = 0
  let n = 0
  while (n < 3) {
    const found = x.indexOf(t, idx)
    if (found < 0) break
    console.log(`\n==== ${t} #${n + 1} ====`)
    console.log(strip(x.slice(found, found + 2000)))
    idx = found + t.length
    n++
  }
}

const markers = ['2.2 地图密钥', '2.3 地图组件', '准备步骤', '基本准备', 'cmmapgl.Map']
for (const m of markers) {
  const i = x.indexOf(m)
  if (i < 0) continue
  console.log(`\n######## SECTION ${m} ########`)
  console.log(strip(x.slice(i, i + 12000)))
}
