@@ -7,14 +7,9 @@ import * as Code from '../code'
7
7
import { Logger } from 'nagato'
8
8
9
9
import URL from 'url-parse'
10
+ import * as CM from 'commonmark'
10
11
11
12
12
- let Marked = require ( 'marked' )
13
- const MarkedOpts = {
14
- gfm : true ,
15
- tables : true ,
16
- }
17
-
18
13
class Meta {
19
14
static PageKey = PageKey
20
15
@@ -119,15 +114,11 @@ class Meta {
119
114
120
115
parse_impl ( content_type , data ) {
121
116
if ( content_type === Net . ContentType . MARKDOWN ) {
122
- let lexer = new Marked . Lexer ( MarkedOpts )
123
- this . log . debug ( 'lexer' , lexer )
124
-
125
- this . tokens = lexer . lex ( data ) . map ( e => {
126
- return new Map ( Object . entries ( e ) )
127
- } )
128
- this . log . debug ( `markdown (${ this . tokens . length } tokens)` , this . tokens )
117
+ let reader = new CM . Parser ( { } )
118
+ const parsed = reader . parse ( data )
119
+ this . log . debug ( 'parsed' , parsed )
129
120
130
- this . process ( this . tokens )
121
+ this . process ( parsed . walker ( ) )
131
122
}
132
123
}
133
124
@@ -138,34 +129,35 @@ class Meta {
138
129
return this . codes . get ( id )
139
130
}
140
131
141
- process ( tokens ) {
132
+ process ( walker ) {
142
133
this . is_first_list = true
143
134
this . single_bufs = [ ]
144
135
145
136
const old_level = this . log . opts . data . ctx . level
146
137
this . log . opts . data . ctx . level = Logger . Level . info
147
138
148
139
try {
149
- for ( const token of tokens ) {
150
- this . process_single ( token )
140
+ let ev = null
141
+ while ( ev = walker . next ( ) ) {
142
+ this . process_single ( ev )
151
143
}
152
144
153
145
} finally {
154
146
this . log . opts . data . ctx . level = old_level
155
147
}
156
148
}
157
149
158
- static isSampleCode ( lang , buf ) {
159
- return lang === 'cpp' && buf . split ( / \n + / ) . some ( ( line ) => line . trim ( ) . match ( / ^ # i n c l u d e / ) )
150
+ static isSampleCode ( lang , info ) {
151
+ return info . includes ( 'example' )
160
152
}
161
153
162
- process_single ( token ) {
163
- this . log . debug ( `processing token <${ token . get ( 'type' ) } >` , token )
154
+ process_single ( ev ) {
155
+ const node = ev . node
156
+ this . log . debug ( `[${ ev . entering ? 'enter' : 'leave' } ] ${ node . type } ` , ev )
164
157
165
- switch ( token . get ( ' type' ) ) {
158
+ switch ( node . type ) {
166
159
case 'heading' : {
167
- this . heading_depth = token . depth
168
- this . heading = token . get ( 'text' ) . trim ( )
160
+ this . heading_depth = node . level
169
161
break
170
162
}
171
163
@@ -201,34 +193,32 @@ class Meta {
201
193
202
194
case 'text' : {
203
195
if ( this . is_first_list ) {
204
- this . single_bufs [ this . single_bufs . length - 1 ] += token . get ( 'text' )
196
+ this . single_bufs [ this . single_bufs . length - 1 ] += node . literal
205
197
}
206
198
break
207
199
}
208
200
209
- case 'code' : {
210
- const lang = token . get ( 'lang' )
211
- const buf = token . get ( 'text' )
212
-
213
- this . log . info ( `found a code section (#${ this . last_key } )` )
201
+ case 'code_block' : {
202
+ const [ lang , ...info ] = node . info . split ( / \s + / )
203
+ this . log . info ( `found a code block (#${ this . last_key } , lang: '${ lang } ', info: ${ info . length ? `[${ info . join ( ', ' ) } ]` : '(empty)' } )` , node )
214
204
215
- if ( ! Meta . isSampleCode ( lang , buf ) ) {
216
- this . log . warn ( `unsupported code snippet (lang: ' ${ lang || '(empty)' } ') ` , buf )
217
- if ( lang ) ++ this . last_key
205
+ if ( ! Meta . isSampleCode ( lang , info ) ) {
206
+ this . log . warn ( `unsupported code snippet` , node )
207
+ ++ this . last_key
218
208
break
219
209
}
220
210
221
211
try {
222
212
if ( lang === 'cpp' ) {
223
- this . log . info ( `got C++ code (#${ this . last_key } )` , buf )
213
+ this . log . info ( `got C++ code (#${ this . last_key } )` , node . literal )
224
214
225
215
const headers = [ this . andareMetaInfo . get ( 'header' ) ] . filter ( Boolean )
226
216
const id = new Code . ID ( 'CPP' , this . last_key )
227
217
this . codes . add (
228
218
new Code . CPP (
229
219
this . log ,
230
220
id ,
231
- buf ,
221
+ node . literal ,
232
222
{
233
223
headers : headers ,
234
224
} ,
@@ -237,7 +227,7 @@ class Meta {
237
227
this . onCodeFound ( id )
238
228
239
229
} else {
240
- this . log . warn ( `got code for unknown language '${ lang } ', skipping...` , buf )
230
+ this . log . warn ( `got code for unknown language '${ lang } ', skipping...` , node . literal )
241
231
}
242
232
243
233
} finally {
0 commit comments