public IActionResult VerifyFull([FromQuery] string lang, [FromQuery] string simp, [FromQuery] string trad, [FromQuery] string pinyin, [FromQuery] string trg) { // Mucho TO-DO in this action: // - Escape slashes in senses // - Proper checking for all sorts of stuff if (simp == null) { return(StatusCode(400, "Missing 'simp' parameter.")); } if (trad == null) { return(StatusCode(400, "Missing 'trad' parameter.")); } if (pinyin == null) { return(StatusCode(400, "Missing 'pinyin' parameter.")); } if (trg == null) { return(StatusCode(400, "Missing 'trg' parameter.")); } NewEntryVerifyFullResult res = new NewEntryVerifyFullResult(); res.Passed = true; CedictEntry entry = Utils.BuildEntry(simp, trad, pinyin, trg); StringBuilder sb = new StringBuilder(); EntryRenderer er = new EntryRenderer(lang, entry, true); er.Render(sb, null); res.Preview = sb.ToString(); // Tell our caller return(new ObjectResult(res)); }
public IActionResult GetEditEntryData([FromQuery] string entryId, [FromQuery] string lang) { if (entryId == null || lang == null) { return(StatusCode(400, "Missing parameter(s).")); } // The data we'll return. EditEntryData res = new EditEntryData(); // Is this an authenticated user? int userId; string userName; auth.CheckSession(HttpContext.Request.Headers, out userId, out userName); // Can she approve entries? if (userId != -1) { res.CanApprove = auth.CanApprove(userId); } // Retrieve entry int idVal = EntryId.StringToId(entryId); string hw, trg; EntryStatus status; SqlDict.GetEntryById(idVal, out hw, out trg, out status); CedictParser parser = new CedictParser(); CedictEntry entry = parser.ParseEntry(hw + " " + trg, 0, null); res.Status = status.ToString().ToLowerInvariant(); res.HeadSimp = entry.ChSimpl; res.HeadTrad = entry.ChTrad; res.HeadPinyin = ""; for (int i = 0; i != entry.PinyinCount; ++i) { if (res.HeadPinyin.Length > 0) { res.HeadPinyin += " "; } var pys = entry.GetPinyinAt(i); res.HeadPinyin += pys.GetDisplayString(false); } res.TrgTxt = trg.Trim('/').Replace('/', '\n').Replace('\\', '/'); // Entry HTML entry.Status = status; EntryRenderer er = new EntryRenderer(lang, entry, true, "mainEntry"); er.OneLineHanziLimit = 12; StringBuilder sb = new StringBuilder(); er.Render(sb, null); res.EntryHtml = sb.ToString(); // Entry history List <ChangeItem> changes = SqlDict.GetEntryChanges(idVal); sb.Clear(); HistoryRenderer.RenderEntryChanges(sb, hw, trg, status, changes, lang); res.HistoryHtml = sb.ToString(); return(new ObjectResult(res)); }
public IActionResult GetEntryPreview([FromQuery] string origHw, [FromQuery] string trad, [FromQuery] string simp, [FromQuery] string pinyin, [FromQuery] string trgTxt, [FromQuery] string lang) { if (origHw == null || trgTxt == null || lang == null) { return(StatusCode(400, "Missing parameter(s).")); } if (trad == null) { trad = ""; } if (simp == null) { simp = ""; } if (pinyin == null) { pinyin = ""; } EditEntryPreviewResult res = new EditEntryPreviewResult(); // Ugly try-catch, but some incorrect input just generates an exception. // We still want to return a meaningful "no preview" response. try { // DBG if (trgTxt.Contains("micu-barf")) { throw new Exception("barf"); } // Validate current headword bool hwParses = validateHeadword(lang, simp, trad, pinyin, res.ErrorsSimp, res.ErrorsTrad, res.ErrorsPinyin); trgTxt = trgTxt.Replace("\r\n", "\n"); trgTxt = trgTxt.Replace('/', '\\'); trgTxt = trgTxt.Replace('\n', '/'); trgTxt = "/" + trgTxt + "/"; // Headword: use original if current headword has errors string hw = trad + " " + simp + " [" + pinyin + "]"; if (!hwParses) { hw = origHw; } CedictParser parser = new CedictParser(); CedictEntry entry = parser.ParseEntry(hw + " " + trgTxt, 0, null); if (entry != null) { EntryRenderer er = new EntryRenderer(lang, entry, true, "mainEntry"); er.OneLineHanziLimit = 12; StringBuilder sb = new StringBuilder(); er.Render(sb, null); res.PreviewHtml = sb.ToString(); } } catch { } return(new ObjectResult(res)); }
public IActionResult VerifyHead([FromQuery] string lang, [FromQuery] string simp, [FromQuery] string trad, [FromQuery] string pinyin) { if (simp == null) { return(StatusCode(400, "Missing 'simp' parameter.")); } if (trad == null) { return(StatusCode(400, "Missing 'trad' parameter.")); } if (pinyin == null) { return(StatusCode(400, "Missing 'pinyin' parameter.")); } NewEntryVerifyHeadResult res = new NewEntryVerifyHeadResult(); StringBuilder sb = new StringBuilder(); // Prepare pinyin as list of proper syllables List <PinyinSyllable> pyList = new List <PinyinSyllable>(); string[] pyRawArr = pinyin.Split(' '); foreach (string pyRaw in pyRawArr) { PinyinSyllable syll = PinyinSyllable.FromDisplayString(pyRaw); if (syll == null) { syll = new PinyinSyllable(pyRaw, -1); } pyList.Add(syll); } string pyInOne = ""; foreach (var syll in pyList) { if (pyInOne != "") { pyInOne += " "; } pyInOne += syll.GetDisplayString(false); } // Is this a dupe? string head = trad + " " + simp + " [" + pyInOne + "]"; CedictEntry existingEntry = SqlDict.GetEntryByHead(head); if (existingEntry != null) { res.Duplicate = true; res.ExistingEntryId = EntryId.IdToString(existingEntry.StableId); EntryRenderer er = new EntryRenderer(lang, existingEntry, true); er.Render(sb, null); res.ExistingEntry = sb.ToString(); return(new ObjectResult(res)); } // OK, we're good. res.Duplicate = false; // Return all entries, CEDICT and HanDeDict, rendered as HTML CedictEntry[] ced, hdd; langRepo.GetEntries(simp, out ced, out hdd); sb.Append("<div id='newEntryRefCED'>"); foreach (CedictEntry entry in ced) { EntryRenderer er = new EntryRenderer(lang, entry, trad, pyList); er.Render(sb, null); } sb.Append("</div>"); sb.Append("<div id='newEntryRefHDD'>"); foreach (CedictEntry entry in hdd) { EntryRenderer er = new EntryRenderer(lang, entry, trad, pyList); er.Render(sb, null); } sb.Append("</div>"); res.RefEntries = sb.ToString(); // Tell our caller return(new ObjectResult(res)); }