Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
优先去除注释
宏的保险
  • Loading branch information
Aeroblast committed Dec 28, 2021
1 parent 798d7c7 commit 79b2ea8
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 315 deletions.
601 changes: 308 additions & 293 deletions AeroNovelTool/src/AtxtProject.cs

Large diffs are not rendered by default.

32 changes: 29 additions & 3 deletions AeroNovelTool/src/func/GenBBcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GenBbcode
public GenBbcode(string dir)
{
project = new AtxtProject(dir);
project.LoadMacro();
project.LoadMacro(AtxtProject.MacroMode.Bbcode);
project.LoadWebImages();
project.CollectSource();
}
Expand Down Expand Up @@ -71,10 +71,12 @@ string GenBody(string[] txt)
const string reg_imgchar = "\\[imgchar\\]((?!http).*?)\\[\\/imgchar\\]";
const string reg_class = "\\[class=(.*?)\\](.*?)\\[\\/class\\]";
const string reg_chapter = "\\[chapter=(.*?)\\](.*?)\\[\\/chapter\\]";
Dictionary<string, string> reg_dic = new Dictionary<string, string>
{
Dictionary<string, string> reg_dic_comment = new Dictionary<string, string>{
{"/\\*.*?\\*/",""},
{"///.*",""},
};
Dictionary<string, string> reg_dic = new Dictionary<string, string>
{
//{"\\[align=(.*?)\\](.*?)\\[\\/align\\]","<p class=\"aligned\" style=\"text-align:$1\">$2</p>"},
{"^\\[center\\](.*?)\\[\\/center\\]$","[align=center]$1[/align]"},
{"^\\[right\\](.*?)\\[\\/right\\]$","[align=right]$1[/align]"},
Expand Down Expand Up @@ -118,21 +120,45 @@ string GenBody(string[] txt)
string r = line;
Match m = Regex.Match("", "1");

do
{
foreach (var pair in reg_dic_comment)
{
m = Regex.Match(r, pair.Key);
if (m.Success)
{
Regex reg = new Regex(pair.Key);
r = reg.Replace(r, pair.Value);
break;
}
}
} while (m.Success);
//macros
if (project.macros != null)
{
int executionCount = 0;
do
{
string safeCheck = r;
foreach (var pair in project.macros)
{
m = Regex.Match(r, pair.Key);
if (m.Success)
{
Regex reg = new Regex(pair.Key);
r = reg.Replace(r, pair.Value);
executionCount++;
if (r == safeCheck) continue;
break;
}
}
if (r == safeCheck) break;
if (executionCount > 100)
{
Log.Error("Macro: Max count");
Log.Error(r);
break;
}
} while (m.Success);
}
if (r.StartsWith("##")) continue;
Expand Down
30 changes: 30 additions & 0 deletions AeroNovelTool/src/func/GenEPUB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public string img_path
{
get { return Path.Combine(dir, "Images"); }
}
public string fnt_path
{
get { return Path.Combine(dir, "Fonts"); }
}
public List<string> img_names = new List<string>();
public ChineseConvertOption cc_option;
public ConfigValue indentAdjust = 0;
Expand Down Expand Up @@ -105,6 +109,7 @@ public EpubFile Gen(string dir)
CollectSource();
GenContent();
GetImage();
GetFont();
GetCss();

TextEpubItemFile toc = epub.GetFile<TextEpubItemFile>("OEBPS/toc.ncx");
Expand Down Expand Up @@ -314,6 +319,31 @@ void GetImage()

}

}
void GetFont()
{
if (Directory.Exists(fnt_path))
{
Dictionary<string, string> fnttype = new Dictionary<string, string>
{
{".otf","application/font-sfnt"},
};
foreach (var f in Directory.GetFiles(fnt_path))
{
string ext = Path.GetExtension(f.ToLower());
string fn = Path.GetFileName(f);
if (fnttype.ContainsKey(ext))
{
EpubItemFile i = new EpubItemFile("OEBPS/Fonts/" + fn, File.ReadAllBytes(f));
epub.items.Add(i);
string properties = "";
items += $" <item id=\"{fn}\" href=\"Fonts/{fn}\" media-type=\"{fnttype[ext]}\"{properties}/>\n";
Log.Info("Add font: " + fn);
}

}
}

}
void GetCss()
{
Expand Down
35 changes: 29 additions & 6 deletions AeroNovelTool/src/func/GenHTML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public string Gen(string[] txt)
const string reg_imgchar = "\\[imgchar\\](.*?)\\[\\/imgchar\\]";
const string reg_class = "\\[class=(.*?)\\](.*?)\\[\\/class\\]";
const string reg_chapter = "\\[chapter=(.*?)\\](.*?)\\[\\/chapter\\]";
Dictionary<string, string> reg_dic_comment = new Dictionary<string, string>{
{"/\\*.*?\\*/",""},
{"///.*",""},
};
Dictionary<string, string> reg_dic = new Dictionary<string, string>
{
{"^\\[align=(.*?)\\](.*?)\\[\\/align\\]$","<p class=\"atxt_aligned\" style=\"text-align:$1\">$2</p>"},
Expand All @@ -39,10 +43,6 @@ public string Gen(string[] txt)
{"^\\[h5\\](.*?)\\[\\/h5\\]$","<h5>$1</h5>"},
{"^\\[h6\\](.*?)\\[\\/h6\\]$","<h6>$1</h6>"},
///以上做旧版兼容,找个时机扫进垃圾堆

///优先去除注释
{"/\\*.*?\\*/",""},
{"///.*",""},

{"^#center:(.*)","<p class=\"atxt_align_center\">$1</p>"},
{"^#right:(.*)","<p class=\"atxt_align_right\">$1</p>"},
Expand All @@ -56,7 +56,7 @@ public string Gen(string[] txt)
{reg_chapter,""},
{"\\[b\\](.*?)\\[\\/b\\]","<b>$1</b>"},
{"^#title:(.*)","<p class=\"atxt_title\">$1</p>"},
{"\\[ruby=(.*?)\\](.*?)\\[\\/ruby\\]","<ruby>$2<rt>$1</rt></ruby>"},
{"\\[ruby=(.*?)\\](.*?)\\[\\/ruby\\]","<ruby>$2<rp>(</rp><rt>$1</rt><rp>)</rp></ruby>"},
{"^\\[pagebreak\\]$","<p class=\"atxt_pagebreak\"><br/></p>"},
{"\\[emphasis\\](.*?)\\[\\/emphasis\\]","<span class=\"atxt_emph\">$1</span>"},
{"\\[s\\](.*?)\\[\\/s\\]","<s>$1</s>"},
Expand All @@ -79,29 +79,52 @@ public string Gen(string[] txt)
{"(?<!<span class=\"atxt_breakall\">)(?<!—)[—]{3,99}","<span class=\"atxt_breakall\">$0</span>"}
};


string html = "";
foreach (string line in txt)
{
if (line.StartsWith("##")) continue;

string r = EncodeHTML(line);
Match m = Regex.Match("", "1");
do
{
foreach (var pair in reg_dic_comment)
{
m = Regex.Match(r, pair.Key);
if (m.Success)
{
Regex reg = new Regex(pair.Key);
r = reg.Replace(r, pair.Value);
break;
}
}
} while (m.Success);

if (context != null & context.macros != null)
{
int executionCount = 0;
do
{
string safeCheck = r;
foreach (var pair in context.macros)
{
m = Regex.Match(r, pair.Key);
if (m.Success)
{
Regex reg = new Regex(pair.Key);
r = reg.Replace(r, pair.Value);
executionCount++;
if (r == safeCheck) continue;
break;
}
}
if (r == safeCheck) break;
if (executionCount > 100)
{
Log.Error("Macro: Max count");
Log.Error(r);
break;
}
} while (m.Success);
}

Expand Down
40 changes: 32 additions & 8 deletions AeroNovelTool/src/func/GenInlineHTML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GenInlineHTML
public GenInlineHTML(string dir)
{
project = new AtxtProject(dir);
project.LoadMacro();
project.LoadMacro(AtxtProject.MacroMode.InlineHTML);
project.LoadWebImages();
project.CollectSource();
}
Expand Down Expand Up @@ -50,13 +50,12 @@ public string GenBody(string[] txt)
const string reg_imgchar = "\\[imgchar\\](.*?)\\[\\/imgchar\\]";
const string reg_class = "\\[class=(.*?)\\](.*?)\\[\\/class\\]";
const string reg_chapter = "\\[chapter=(.*?)\\](.*?)\\[\\/chapter\\]";
Dictionary<string, string> reg_dic = new Dictionary<string, string>
{

///优先去除注释
Dictionary<string, string> reg_dic_comment = new Dictionary<string, string>{
{"/\\*.*?\\*/",""},
{"///.*",""},

};
Dictionary<string, string> reg_dic = new Dictionary<string, string>
{
{"^#center:(.*)","<p style=\"text-align:center;margin:0;\">$1</p>"},
{"^#right:(.*)","<p style=\"text-align:right;margin:0;\">$1</p>"},
{"^#left:(.*)","<p style=\"text-align:left;margin:0;\">$1</p>"},
Expand All @@ -69,7 +68,7 @@ public string GenBody(string[] txt)
{reg_chapter,"$2"},
{"\\[b\\](.*?)\\[\\/b\\]","<b>$1</b>"},
{"^#title:(.*)","<p style=\"text-align:center;font-size:1.6em;font-weight:bold\">$1</p>"},
{"\\[ruby=(.*?)\\](.*?)\\[\\/ruby\\]","<ruby>$2<rt>$1</rt></ruby>"},
{"\\[ruby=(.*?)\\](.*?)\\[\\/ruby\\]","<ruby>$2<rp>(</rp><rt>$1</rt><rp>)</rp></ruby>"},
{"^\\[pagebreak\\]$","<p class=\"atxt_pagebreak\"><br/></p>"},
{"\\[em\\](.*?)\\[\\/em\\]","<span style=\"-webkit-text-emphasis: dot filled;-webkit-text-emphasis-position: under;\">$1</span>"},
{"\\[s\\](.*?)\\[\\/s\\]","<s>$1</s>"},
Expand All @@ -82,7 +81,7 @@ public string GenBody(string[] txt)
{"^#h4:(.*)","<h4>$1</h4>"},
{"^#h5:(.*)","<h5>$1</h5>"},
{"^#h6:(.*)","<h6>$1</h6>"},
{"^#class:(.*)","<div class='$1'>"},
{"^#class:(.*)","<div class=\"$1\">"},
{"^#/class","</div>"},
{"\\[font\\](.*?)\\[\\/font\\]","<span class=\"atxt_font\">$1</span>"},
{"\\[url=(.*?)\\](.*?)\\[\\/url\\]","<a href=\"$1\">$2</a>"},
Expand All @@ -99,22 +98,47 @@ public string GenBody(string[] txt)
if (line.StartsWith("##")) continue;

string r = EncodeHTML(line);

Match m = Regex.Match("", "1");
do
{
foreach (var pair in reg_dic_comment)
{
m = Regex.Match(r, pair.Key);
if (m.Success)
{
Regex reg = new Regex(pair.Key);
r = reg.Replace(r, pair.Value);
break;
}
}
} while (m.Success);

if (project.macros != null)
{
int executionCount = 0;
do
{
string safeCheck = r;
foreach (var pair in project.macros)
{
m = Regex.Match(r, pair.Key);
if (m.Success)
{
Regex reg = new Regex(pair.Key);
r = reg.Replace(r, pair.Value);
executionCount++;
if (r == safeCheck) continue;
break;
}
}
if (r == safeCheck) break;
if (executionCount > 100)
{
Log.Error("Macro: Max count");
Log.Error(r);
break;
}
} while (m.Success);
}

Expand Down
23 changes: 20 additions & 3 deletions AeroNovelTool/src/func/GlossaryImportation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ string TranslateLine(string line)
if (t1 != null) { temp[i] = t1; }
else
{
if (!string.IsNullOrEmpty(temp[i].output))
string tryGetOutput = GetOutput(temp[i]);
if (!string.IsNullOrEmpty(tryGetOutput))
{
TryAddSpace(ref result, temp[i].output);
result += temp[i].output;
TryAddSpace(ref result, tryGetOutput);
result += tryGetOutput;
temp.Clear();
break;
}
Expand Down Expand Up @@ -108,6 +109,19 @@ CharNode FindNodeByChar(List<CharNode> nodes, char c)
return null;
}

string GetOutput(CharNode node)
{
do
{
if (!string.IsNullOrEmpty(node.output))
{
return node.output;
}
node = node.parent;
} while (node.parent != null);
return null;
}

void TryAddSpace(ref string result, string added)
{
if (result != "")
Expand Down Expand Up @@ -139,13 +153,16 @@ bool isBracket(char c)
private class CharNode
{
public char v;
public CharNode parent = null;
public CharNode(char c) { v = c; }
public CharNode AddChild(char c)
{
CharNode child = new CharNode(c);
child.parent = this;
children.Add(child);
return child;
}

public List<CharNode> children = new List<CharNode>();
public string output = "";

Expand Down
2 changes: 1 addition & 1 deletion AeroNovelTool/src/version.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Version
{
public static string date = "20211201";
public static string date = "20211226";
}
2 changes: 1 addition & 1 deletion AeroNovelTool/template/OEBPS/Styles/Style.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ p {

.atxt_illu {
width: 100%;
height: 100vh;
height: 99vh;
page-break-inside: avoid;
display: flex;
align-items: center;
Expand Down

0 comments on commit 79b2ea8

Please sign in to comment.