@@ -44,8 +44,7 @@ public Epub(Azw3File azw3, Azw6File azw6 = null)
44
44
}
45
45
try
46
46
{
47
- CreateNCX ( ) ;
48
- CreateNAV ( ) ;
47
+ CreateIndexDoc ( ) ;
49
48
}
50
49
catch ( Exception e )
51
50
{
@@ -118,6 +117,7 @@ void ProcNodes(XmlNode node)
118
117
if ( node . Attributes != null )
119
118
{
120
119
node . Attributes . RemoveNamedItem ( "aid" ) ;
120
+ node . Attributes . RemoveNamedItem ( "amzn-src-id" ) ;
121
121
foreach ( XmlAttribute attr in node . Attributes )
122
122
{
123
123
if ( attr . Value . IndexOf ( "kindle:" ) == 0 )
@@ -233,7 +233,7 @@ void ProcLink(XmlAttribute attr)
233
233
}
234
234
string KindlePosToUri ( int fid , int off ) //务必在插入封面前调用
235
235
{
236
- Regex reg_html_id = new Regex ( "<.*? id=\" (.*?)\" .*?>" ) ;
236
+ Regex reg_html_id = new Regex ( "^ <.*? id=\" (.*?)\" .*?>" ) ;
237
237
Fragment_item frag = azw3 . frag_table [ fid ] ;
238
238
byte [ ] t = Util . SubArray ( azw3 . rawML , frag . pos_in_raw + off , frag . length - off ) ;
239
239
string s = Encoding . UTF8 . GetString ( t ) ;
@@ -275,52 +275,106 @@ string ProcCSS(string text)
275
275
return r ;
276
276
}
277
277
278
- void CreateNCX ( )
278
+ private class IndexNode
279
279
{
280
- string t = File . ReadAllText ( "template\\ template_ncx.txt" ) ;
281
- string np_temp = "<navPoint id=\" navPoint-{0}\" playOrder=\" {0}\" >\n <navLabel><text>{1}</text></navLabel>\n <content src=\" {2}\" />\n </navPoint>\n " ;
282
- string np = "" ;
283
- int i = 1 ;
284
- if ( azw3 . ncx_table != null )
285
- foreach ( NCX_item info in azw3 . ncx_table )
286
- {
287
- np += String . Format ( np_temp , i , info . title , "Text/" + KindlePosToUri ( info . fid , info . off ) ) ;
288
- i ++ ;
289
- }
290
- t = t . Replace ( "{❕navMap}" , np ) ;
291
- t = t . Replace ( "{❕Title}" , azw3 . title ) ;
292
- string z = azw3 . mobi_header . extMeta . id_string [ 504 ] ; //ASIN
293
- t = t . Replace ( "{❕uid}" , z ) ;
294
- ncx = t ;
280
+ public string href , title ;
281
+ public List < IndexNode > children ;
282
+ public IndexNode parent ;
283
+ public IndexNode ( string href , string title )
284
+ {
285
+ this . href = href ;
286
+ this . title = title ;
287
+ }
288
+
295
289
}
296
- void CreateNAV ( )
290
+ int playOrder = 1 ;
291
+ void CreateIndexDoc_Helper ( IndexNode node , StringBuilder temp_epub3 , StringBuilder temp_epub2 , int level = 0 )
297
292
{
298
- string t = File . ReadAllText ( "template\\ template_nav.txt" ) ;
299
- string np_temp = " <li><a href=\" {1}\" >{0}</a></li>\n " ;
300
- string np = "" ;
301
- if ( azw3 . ncx_table != null )
302
- foreach ( NCX_item info in azw3 . ncx_table )
293
+ string tabs = new String ( '\t ' , level ) ;
294
+
295
+ temp_epub3 . Append ( "\n " + tabs + $ "<ol>\n ") ;
296
+ foreach ( var n in node . children )
297
+ {
298
+
299
+ temp_epub3 . Append ( tabs + $ "<li><a href=\" { n . href } \" >{ n . title } </a>") ;
300
+
301
+ temp_epub2 . Append ( tabs + $ "<navPoint id=\" navPoint-{ playOrder } \" playOrder=\" { playOrder } \" >\n ") ;
302
+ temp_epub2 . Append ( tabs + $ "\t <navLabel><text>{ n . title } </text></navLabel>\n ") ;
303
+ temp_epub2 . Append ( tabs + $ "\t <content src=\" { n . href } \" />\n ") ;
304
+
305
+ playOrder ++ ;
306
+
307
+ if ( n . children != null )
303
308
{
304
- np += String . Format ( np_temp , info . title , "Text/" + KindlePosToUri ( info . fid , info . off ) ) ;
309
+ CreateIndexDoc_Helper ( n , temp_epub3 , temp_epub2 , level + 1 ) ;
305
310
}
306
- t = t . Replace ( "{❕toc}" , np ) ;
307
- string guide = "" ;
308
- if ( azw3 . guide_table != null )
309
- foreach ( Guide_item g in azw3 . guide_table )
311
+ temp_epub3 . Append ( "</li>\n " ) ;
312
+ temp_epub2 . Append ( tabs + "</navPoint>\n " ) ;
313
+ }
314
+ temp_epub3 . Append ( tabs + $ "</ol>\n ") ;
315
+ }
316
+ void CreateIndexDoc ( )
317
+ {
318
+ List < IndexNode > allEntries = new List < IndexNode > ( ) ;
319
+ IndexNode root = new IndexNode ( "" , "" ) ;
320
+ root . children = new List < IndexNode > ( ) ;
321
+ int maxLevel = 0 ;
322
+ if ( azw3 . index_info_table != null )
323
+ for ( int i = 0 ; i < azw3 . index_info_table . Count ; i ++ )
310
324
{
311
- try
325
+ IndexInfo_item info = azw3 . index_info_table [ i ] ;
326
+ var entry = new IndexNode ( "Text/" + KindlePosToUri ( info . fid , info . off ) , info . title ) ;
327
+ allEntries . Add ( entry ) ;
328
+ if ( info . children_start != - 1 ) { entry . children = new List < IndexNode > ( ) ; }
329
+ if ( info . level > 0 )
312
330
{
313
- guide += string . Format ( " <li><a epub:type=\" {2}\" href=\" {1}\" >{0}</a></li>\n " , g . ref_name , Path . Combine ( "Text/" , xhtml_names [ azw3 . frag_table [ g . num ] . file_num + 1 ] ) , g . ref_type ) ;
331
+ entry . parent = allEntries [ info . parent ] ;
332
+ entry . parent . children . Add ( entry ) ;
333
+
334
+ //assert
335
+ var _item = azw3 . index_info_table [ info . parent ] ;
336
+ if ( _item . children_start > i || _item . children_end < i ) { throw new Exception ( "Index Error" ) ; }
314
337
}
315
- catch ( Exception e )
338
+ else
316
339
{
317
- Log . log ( "Error at Gen guide." ) ;
318
- Log . log ( e . ToString ( ) ) ;
340
+ root . children . Add ( entry ) ;
319
341
}
342
+ if ( info . level > maxLevel ) maxLevel = info . level ;
320
343
}
344
+ StringBuilder temp_epub3 = new StringBuilder ( ) , temp_epub2 = new StringBuilder ( ) ;
345
+ CreateIndexDoc_Helper ( root , temp_epub3 , temp_epub2 ) ;
346
+ //Create NAV
347
+ {
348
+ string t = File . ReadAllText ( "template\\ template_nav.txt" ) ;
349
+ t = t . Replace ( "{❕toc}" , temp_epub3 . ToString ( ) ) ;
350
+ string guide = "" ;
351
+ if ( azw3 . guide_table != null )
352
+ foreach ( Guide_item g in azw3 . guide_table )
353
+ {
354
+ try
355
+ {
356
+ guide += string . Format ( " <li><a epub:type=\" {2}\" href=\" {1}\" >{0}</a></li>\n " , g . ref_name , Path . Combine ( "Text/" , xhtml_names [ azw3 . frag_table [ g . num ] . file_num + 1 ] ) , g . ref_type ) ;
357
+ }
358
+ catch ( Exception e )
359
+ {
360
+ Log . log ( "Error at Gen guide." ) ;
361
+ Log . log ( e . ToString ( ) ) ;
362
+ }
363
+ }
321
364
322
- t = t . Replace ( "{❕guide}" , guide ) ;
323
- nav = t ;
365
+ t = t . Replace ( "{❕guide}" , guide ) ;
366
+ nav = t ;
367
+ }
368
+ {
369
+ string t = File . ReadAllText ( "template\\ template_ncx.txt" ) ;
370
+
371
+ t = t . Replace ( "{❕navMap}" , temp_epub2 . ToString ( ) ) ;
372
+ t = t . Replace ( "{❕Title}" , azw3 . title ) ;
373
+ string z = azw3 . mobi_header . extMeta . id_string [ 504 ] ; //ASIN
374
+ t = t . Replace ( "{❕uid}" , z ) ;
375
+ t = t . Replace ( "{❕depth}" , maxLevel + 1 + "" ) ;
376
+ ncx = t ;
377
+ }
324
378
}
325
379
void CreateCover ( )
326
380
{
0 commit comments