-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml2json.coffee
executable file
·48 lines (33 loc) · 1.03 KB
/
xml2json.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/env coffee
parser = require "xml2json"
fs = require "fs"
_ = require "underscore"
wiki = process.argv.slice(2)[0]
wiki = "hitchwiki" unless wiki
console.log "Processing", wiki
fs.readFile "dump.xml", (err, data) ->
xml = data
console.log "xml size:", data.length
# E.g. English Wikivoyage
if data.length < 100000000
json_out = parser.toJson xml,
sanitize: false
trim: false
# We can make it smaller
else
json = parser.toJson xml,
sanitize: false
trim: false
js = JSON.parse json
jsOut = {}
jsOut.siteinfo = js.mediawiki.siteinfo
console.log 'all pages: ', js.mediawiki.page.length
jsOut.page = _.filter js.mediawiki.page, (p) ->
# Main, MediaWiki, Template or Category
_.contains [0, 8, 10, 14], p.ns
console.log 'ns 0, 10, 14: ', jsOut.page.length
json_out = JSON.stringify jsOut
console.log "json size:", json_out.length
fs.writeFile "dump.js", "jsondump = " + json_out, (err) ->
throw err if err
console.log "saved dump.js"