Skip to content

Commit

Permalink
支持目录层级
Browse files Browse the repository at this point in the history
.net5
增加一个删除属性 amzn-src-id
  • Loading branch information
Aeroblast committed Jan 7, 2021
1 parent 0c6dad1 commit f736c74
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 54 deletions.
6 changes: 3 additions & 3 deletions publish.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ rd /Q /S bin
cd src
rd /Q /S bin

dotnet publish -c release -r win10-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true
dotnet publish -c Release -r win10-x64

md ..\bin
del bin\release\netcoreapp3.1\win10-x64\publish\*.pdb
move bin\release\netcoreapp3.1\win10-x64\publish ..\bin\app
del bin\release\net5\win10-x64\publish\*.pdb
move bin\release\net5\win10-x64\publish ..\bin\app

md ..\bin\app\template\
copy template\template_cover.txt ..\bin\app\template\template_cover.txt
Expand Down
35 changes: 29 additions & 6 deletions src/Azw3File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void ProcessRes()
public List<Skeleton_item> skeleton_table;
public List<Fragment_item> frag_table;
public List<Guide_item> guide_table;
public List<NCX_item> ncx_table;
public List<IndexInfo_item> index_info_table;
void ProcessIndex()
{

Expand Down Expand Up @@ -230,7 +230,7 @@ void ProcessIndex()
}
if (mobi_header.ncx_index != 0xffffffff)
{
ncx_table = new List<NCX_item>();
index_info_table = new List<IndexInfo_item>();
Hashtable ctoc_dict = new Hashtable();
INDX_Section_Main main_indx = new INDX_Section_Main(GetSectionData(mobi_header.ncx_index), "INDX(NCX)");
sections[mobi_header.ncx_index] = main_indx;
Expand All @@ -255,19 +255,42 @@ void ProcessIndex()
ext_indx.ReadTagMap();
for (int j = 0; j < ext_indx.tagmaps.Length; j++)
{
NCX_item item = new NCX_item();
IndexInfo_item item = new IndexInfo_item();
item.name = ext_indx.texts[j];
foreach (var k in ext_indx.tagmaps[j])
{
List<int> a = (List<int>)((DictionaryEntry)k).Value;
item.fid = a[4];
item.off = a[5];
item.position = a[0];
item.length = a[1];
item.title = (string)ctoc_dict[a[2]];
item.level = a[3];
if (item.level > 0)
{
item.parent = a[4];
item.fid = a[5];
item.off = a[6];
}
else
{
switch (a.Count)
{
case 6:
item.fid = a[4];
item.off = a[5];
break;
case 8:
item.children_start = a[4];
item.children_end = a[5];
item.fid = a[6];
item.off = a[7];
break;
default: throw new Exception("Unhandled Error at INDX");
}
}
break;
}
ncx_table.Add(item);
index_info_table.Add(item);
Console.WriteLine($"{item.name} {item.fid} {item.off} {item.title}");
}
}
}
Expand Down
128 changes: 91 additions & 37 deletions src/EpubBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public Epub(Azw3File azw3, Azw6File azw6 = null)
}
try
{
CreateNCX();
CreateNAV();
CreateIndexDoc();
}
catch (Exception e)
{
Expand Down Expand Up @@ -118,6 +117,7 @@ void ProcNodes(XmlNode node)
if (node.Attributes != null)
{
node.Attributes.RemoveNamedItem("aid");
node.Attributes.RemoveNamedItem("amzn-src-id");
foreach (XmlAttribute attr in node.Attributes)
{
if (attr.Value.IndexOf("kindle:") == 0)
Expand Down Expand Up @@ -233,7 +233,7 @@ void ProcLink(XmlAttribute attr)
}
string KindlePosToUri(int fid, int off)//务必在插入封面前调用
{
Regex reg_html_id = new Regex("<.*? id=\"(.*?)\".*?>");
Regex reg_html_id = new Regex("^<.*? id=\"(.*?)\".*?>");
Fragment_item frag = azw3.frag_table[fid];
byte[] t = Util.SubArray(azw3.rawML, frag.pos_in_raw + off, frag.length - off);
string s = Encoding.UTF8.GetString(t);
Expand Down Expand Up @@ -275,52 +275,106 @@ string ProcCSS(string text)
return r;
}

void CreateNCX()
private class IndexNode
{
string t = File.ReadAllText("template\\template_ncx.txt");
string np_temp = "<navPoint id=\"navPoint-{0}\" playOrder=\"{0}\">\n <navLabel><text>{1}</text></navLabel>\n <content src=\"{2}\" />\n</navPoint>\n";
string np = "";
int i = 1;
if (azw3.ncx_table != null)
foreach (NCX_item info in azw3.ncx_table)
{
np += String.Format(np_temp, i, info.title, "Text/" + KindlePosToUri(info.fid, info.off));
i++;
}
t = t.Replace("{❕navMap}", np);
t = t.Replace("{❕Title}", azw3.title);
string z = azw3.mobi_header.extMeta.id_string[504];//ASIN
t = t.Replace("{❕uid}", z);
ncx = t;
public string href, title;
public List<IndexNode> children;
public IndexNode parent;
public IndexNode(string href, string title)
{
this.href = href;
this.title = title;
}

}
void CreateNAV()
int playOrder = 1;
void CreateIndexDoc_Helper(IndexNode node, StringBuilder temp_epub3, StringBuilder temp_epub2, int level = 0)
{
string t = File.ReadAllText("template\\template_nav.txt");
string np_temp = " <li><a href=\"{1}\">{0}</a></li>\n";
string np = "";
if (azw3.ncx_table != null)
foreach (NCX_item info in azw3.ncx_table)
string tabs = new String('\t', level);

temp_epub3.Append("\n" + tabs + $"<ol>\n");
foreach (var n in node.children)
{

temp_epub3.Append(tabs + $"<li><a href=\"{n.href}\">{n.title}</a>");

temp_epub2.Append(tabs + $"<navPoint id=\"navPoint-{playOrder}\" playOrder=\"{playOrder}\">\n");
temp_epub2.Append(tabs + $"\t<navLabel><text>{n.title}</text></navLabel>\n");
temp_epub2.Append(tabs + $"\t<content src=\"{n.href}\" />\n");

playOrder++;

if (n.children != null)
{
np += String.Format(np_temp, info.title, "Text/" + KindlePosToUri(info.fid, info.off));
CreateIndexDoc_Helper(n, temp_epub3, temp_epub2, level + 1);
}
t = t.Replace("{❕toc}", np);
string guide = "";
if (azw3.guide_table != null)
foreach (Guide_item g in azw3.guide_table)
temp_epub3.Append("</li>\n");
temp_epub2.Append(tabs + "</navPoint>\n");
}
temp_epub3.Append(tabs + $"</ol>\n");
}
void CreateIndexDoc()
{
List<IndexNode> allEntries = new List<IndexNode>();
IndexNode root = new IndexNode("", "");
root.children = new List<IndexNode>();
int maxLevel = 0;
if (azw3.index_info_table != null)
for (int i = 0; i < azw3.index_info_table.Count; i++)
{
try
IndexInfo_item info = azw3.index_info_table[i];
var entry = new IndexNode("Text/" + KindlePosToUri(info.fid, info.off), info.title);
allEntries.Add(entry);
if (info.children_start != -1) { entry.children = new List<IndexNode>(); }
if (info.level > 0)
{
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);
entry.parent = allEntries[info.parent];
entry.parent.children.Add(entry);

//assert
var _item = azw3.index_info_table[info.parent];
if (_item.children_start > i || _item.children_end < i) { throw new Exception("Index Error"); }
}
catch (Exception e)
else
{
Log.log("Error at Gen guide.");
Log.log(e.ToString());
root.children.Add(entry);
}
if (info.level > maxLevel) maxLevel = info.level;
}
StringBuilder temp_epub3 = new StringBuilder(), temp_epub2 = new StringBuilder();
CreateIndexDoc_Helper(root, temp_epub3, temp_epub2);
//Create NAV
{
string t = File.ReadAllText("template\\template_nav.txt");
t = t.Replace("{❕toc}", temp_epub3.ToString());
string guide = "";
if (azw3.guide_table != null)
foreach (Guide_item g in azw3.guide_table)
{
try
{
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);
}
catch (Exception e)
{
Log.log("Error at Gen guide.");
Log.log(e.ToString());
}
}

t = t.Replace("{❕guide}", guide);
nav = t;
t = t.Replace("{❕guide}", guide);
nav = t;
}
{
string t = File.ReadAllText("template\\template_ncx.txt");

t = t.Replace("{❕navMap}", temp_epub2.ToString());
t = t.Replace("{❕Title}", azw3.title);
string z = azw3.mobi_header.extMeta.id_string[504];//ASIN
t = t.Replace("{❕uid}", z);
t = t.Replace("{❕depth}", maxLevel + 1 + "");
ncx = t;
}
}
void CreateCover()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Structs&Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected byte[] GetSectionData(uint i)
}
protected AzwFile(string path)
{
raw_data = File.ReadAllBytes(path);
raw_data = File.ReadAllBytes(path);
GetSectionInfo();

}
Expand Down Expand Up @@ -105,10 +105,10 @@ public class Guide_item
public int num;
public Guide_item(string _ref_type, string _ref_name, int no) { ref_type = _ref_type; ref_name = _ref_name; num = no; }
}
public class NCX_item
public class IndexInfo_item
{
public string title, name;
public int fid, off, position, length;
public int fid, off, position, length, level, parent = -1, children_start = -1, children_end = -1;
}
public class IdMapping
{
Expand Down
10 changes: 9 additions & 1 deletion src/UnpackKindleS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>Link</TrimMode>
</PropertyGroup>

</Project>
2 changes: 0 additions & 2 deletions src/template/template_nav.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

<nav epub:type="toc" id="toc">
<h1>Navigation</h1>
<ol>
{❕toc}
</ol>
</nav>

<nav epub:type="landmarks" id="guide">
Expand Down
2 changes: 1 addition & 1 deletion src/template/template_ncx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta name="dtb:uid" content="{❕uid}" />
<meta name="dtb:depth" content="1" />
<meta name="dtb:depth" content="{❕depth}" />
<meta name="dtb:totalPageCount" content="0" />
<meta name="dtb:maxPageNumber" content="0" />
</head>
Expand Down
2 changes: 1 addition & 1 deletion src/version.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace UnpackKindleS
{
public class Version{public static string version="20201007";}
public class Version{public static string version="20210107";}
}

0 comments on commit f736c74

Please sign in to comment.