Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
support tag ref
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed May 30, 2020
1 parent 3278f56 commit 7a4fd86
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 13 deletions.
2 changes: 1 addition & 1 deletion EhDbReleaseBuilder/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"EhDbReleaseBuilder": {
"commandName": "Project",
"commandLineArgs": "--source C:/Users/lzy/Documents/Source/EhTagTranslation/EhTagConnector/EHTT-DB/ --target C:/Users/lzy/Documents/Source/EhTagTranslation/EhTagConnector/EHTT-DB/publish --check-tags Parody,Character,Group,Artist"
"commandLineArgs": "--source C:\\Users\\lzy\\Documents\\Source\\EhTagTranslation\\Database --target C:\\Users\\lzy\\Documents\\Source\\EhTagTranslation\\Database\\publish "
}
}
}
2 changes: 2 additions & 0 deletions EhTagApi/Controllers/ToolsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public IActionResult Status()
[HttpPost("normalize")]
public IActionResult Normalize([FromBody] Record record)
{
record.Render("");
return Ok(record);
}

Expand Down Expand Up @@ -53,6 +54,7 @@ public IActionResult Parse([FromBody][Required] string tableRow)
error.AddModelError(nameof(tableRow), "Failed to parse it as a markdown table row.");
return BadRequest(error);
}
r.Value.Render(r.Key);
return Ok(r);
}
}
Expand Down
1 change: 1 addition & 0 deletions EhTagApi/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public IActionResult Post(
log = $"Pulled form github in {(DateTimeOffset.Now - start).TotalMilliseconds}ms.";
logger.LogInformation(log);
database.Load();
database.Render();
}
else
{
Expand Down
14 changes: 14 additions & 0 deletions EhTagClient/Context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace EhTagClient
{
static class Context
{
public static Database Database { get; set; }
public static Namespace Namespace { get; set; }
public static string Raw { get; set; }
public static Record Record { get; set; }
}
}
10 changes: 10 additions & 0 deletions EhTagClient/DataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ public void Load()
}
}

public void Render()
{
Context.Database = this;
foreach (var item in Values)
{
item.Render();
}
}

public void Save()
{
Context.Database = this;
foreach (var item in Values)
{
item.Save();
Expand Down
21 changes: 21 additions & 0 deletions EhTagClient/MarkdigExt/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,26 @@ public static (string url, string title, string isNsfw) GetData(this LinkInline

return (url, title, null);
}
public static Record GetTag(string tag, Namespace ns)
{
var table = Context.Database[ns];
return table.Data[tag];
}

public static Record GetTag(string tag)
{
var record = GetTag(tag, Context.Namespace);
if (record != null) return record;
foreach (var item in Context.Database.Keys)
{
if (item != Context.Namespace)
{
record = GetTag(tag, item);
if (record != null) return record;
}
}
Console.WriteLine($"Invalid tag ref {tag}");
return null;
}
}
}
40 changes: 40 additions & 0 deletions EhTagClient/MarkdigExt/Html/CodeInlineRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax.Inlines;
using System;

namespace EhTagClient.MarkdigExt.Html
{
class CodeInlineRenderer : HtmlObjectRenderer<CodeInline>
{
protected override void Write(HtmlRenderer renderer, CodeInline obj)
{
var content = obj.Content;
var tag = Extension.GetTag(content);
if (renderer.EnableHtmlForInline)
renderer.Write("<ruby>");
if (tag != null)
{
renderer.WriteEscape(tag.Name.Text);
if (renderer.EnableHtmlForInline)
{
renderer.Write("<rp>(</rp><rt>");
renderer.WriteEscape(content);
renderer.Write("</rt><rp>)</rp>");
}
else
{
renderer.Write('(');
renderer.WriteEscape(content);
renderer.Write(')');
}
}
else
{
renderer.WriteEscape(content);
}
if (renderer.EnableHtmlForInline)
renderer.Write("</ruby>");
}
}
}
2 changes: 0 additions & 2 deletions EhTagClient/MarkdigExt/Html/EhLinkInlineRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax.Inlines;
using System;
using System.Collections.Generic;
using System.Text;

namespace EhTagClient.MarkdigExt.Html
{

class EhLinkInlineRenderer : HtmlObjectRenderer<LinkInline>
{
protected override void Write(HtmlRenderer renderer, LinkInline link)
Expand Down
17 changes: 15 additions & 2 deletions EhTagClient/MarkdigExt/Json/JsonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,22 @@ protected override void WriteData(JsonRenderer renderer, AutolinkInline obj)

sealed class CodeInlineRenderer : JsonObjectRenderer<CodeInline>
{
protected override string GetType(JsonRenderer renderer, CodeInline obj) => "code";
protected override string GetType(JsonRenderer renderer, CodeInline obj) => "tagref";

protected override void WriteContent(JsonRenderer renderer, CodeInline obj) => renderer.WriteProperty("text", obj.Content);
protected override void WriteContent(JsonRenderer renderer, CodeInline obj)
{
var content = obj.Content;
var tag = Extension.GetTag(content);
if (tag != null)
{
renderer.WriteProperty("tag", content);
renderer.WriteProperty("text", tag.Name.Text);
}
else
{
renderer.WriteProperty("text", content);
}
}
}

sealed class DelimiterInlineRenderer : JsonTextRender<DelimiterInline>
Expand Down
1 change: 1 addition & 0 deletions EhTagClient/MarkdigExt/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private static HtmlRenderer _CreateHtmlRenderer(TextWriter writer, bool enableHt
htmlRenderer.EnableHtmlForBlock = false;
htmlRenderer.EnableHtmlForInline = false;
}
htmlRenderer.ObjectRenderers.Replace<Hr.Inlines.CodeInlineRenderer>(new Html.CodeInlineRenderer());
htmlRenderer.ObjectRenderers.Replace<Hr.Inlines.LinkInlineRenderer>(new Html.EhLinkInlineRenderer());
htmlRenderer.ObjectRenderers.Replace<Hr.Inlines.HtmlEntityInlineRenderer>(new Html.HtmlEntityInlineRenderer());
htmlRenderer.ObjectRenderers.Replace<Hr.Inlines.LiteralInlineRenderer>(new Html.LiteralInlineRenderer());
Expand Down
14 changes: 9 additions & 5 deletions EhTagClient/MarkdownText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ public MarkdownText(string rawString, bool singleLine)
rawString = (rawString ?? "").Trim();
rawString = Regex.Replace(rawString, "(\r\n|\r|\n)", singleLine ? " " : "\n").Trim();
Raw = rawString;
}

public string Raw { get; private set; }
public string Text { get; private set; }
public string Html { get; private set; }
public Newtonsoft.Json.Linq.JRaw Ast { get; private set; }

public void Render()
{
var ast = MarkdigExt.Renderer.Parse(Raw);
Text = MarkdigExt.Renderer.ToPlainText(ast);
Raw = MarkdigExt.Renderer.ToNormalizedMarkdown(ast);
Html = MarkdigExt.Renderer.ToHtml(ast);
Ast = new Newtonsoft.Json.Linq.JRaw(MarkdigExt.Renderer.ToJson(ast));
}

public string Raw { get; }
public string Text { get; }
public string Html { get; }
public Newtonsoft.Json.Linq.JRaw Ast { get; }
}
}
17 changes: 15 additions & 2 deletions EhTagClient/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public static KeyValuePair<string, Record> TryParse(string line)
var name = match.Groups[nameof(Name)].Value;
var intro = match.Groups[nameof(Intro)].Value;
var links = match.Groups[nameof(Links)].Value;
return KeyValuePair.Create(raw, new Record(_Unescape(name), _Unescape(intro), _Unescape(links)));
var record = new Record(_Unescape(name), _Unescape(intro), _Unescape(links));
return KeyValuePair.Create(raw, record);
}

private static readonly Regex _UnescapeRe1 = new Regex(@"<br\s*/?>", RegexOptions.Compiled);
Expand Down Expand Up @@ -113,7 +114,19 @@ public Record(string name, string intro, string links)

public MarkdownText Links { get; }

public void Render(string raw)
{
Context.Raw = raw;
Context.Record = this;
Name.Render();
Intro.Render();
Links.Render();
}

public string ToString(string raw)
=> $"| {raw.Trim().ToLower()} | {_Escape(Name.Raw)} | {_Escape(Intro.Raw)} | {_Escape(Links.Raw)} |";
{
Render(raw);
return $"| {raw.Trim().ToLower()} | {_Escape(Name.Raw)} | {_Escape(Intro.Raw)} | {_Escape(Links.Raw)} |";
}
}
}
17 changes: 16 additions & 1 deletion EhTagClient/RecordDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,20 @@ public void Load()
}
}

public void Render()
{
Context.Namespace = Namespace;
foreach (var item in RawData)
{
if (item.Key is null)
continue;
item.Value.Render(item.Key);
}
}

public void Save()
{
Context.Namespace = Namespace;
using (var sw = new StreamWriter(FilePath) { NewLine = "\n" })
{
sw.Write(Prefix);
Expand All @@ -208,7 +220,10 @@ public Record Find(string key)
{
if (!MapData.TryGetValue(key, out var index))
return null;
return RawData[index].Value;
Context.Namespace = Namespace;
var record = RawData[index].Value;
record.Render(key);
return record;
}

public void Add(string key, Record record)
Expand Down

0 comments on commit 7a4fd86

Please sign in to comment.