/// <summary> /// Gets the entry formatted in HTML. /// </summary> public static string GetHtml(ITextProvider tprov, CedictEntry entry, SearchScript script) { StringBuilder bodyHtml = new StringBuilder(); // Are we showing one or two Hanzi headwords? string hanzi1 = script == SearchScript.Traditional ? entry.ChTrad : entry.ChSimpl; string hanzi2 = null; if (script == SearchScript.Both && entry.ChSimpl != entry.ChTrad) hanzi2 = entry.ChTrad; // Find simplest possible template, work with that // Only one hanzi, no longer than 2 chars, only one sense bool mustDoSenses = true; if (hanzi2 == null && hanzi1.Length <= 2 && entry.SenseCount == 1) { mustDoSenses = false; bodyHtml.Append(template1); bodyHtml.Replace("{hanzi}", escape(hanzi1)); bodyHtml.Replace("{pinyin}", escape(GetPinyinString(entry.GetPinyinForDisplay(true)))); bodyHtml.Replace("{sense}", getSenseHtmlPure(tprov, entry.GetSenseAt(0), script)); } // Only one script, no more than 6 chars else if (hanzi2 == null && hanzi1.Length <= 6) { bodyHtml.Append(template2); bodyHtml.Replace("{hanzi}", escape(hanzi1)); bodyHtml.Replace("{pinyin}", escape(GetPinyinString(entry.GetPinyinForDisplay(true)))); } // Only one script else if (hanzi2 == null) { bodyHtml.Append(template3); bodyHtml.Replace("{hanzi}", escape(hanzi1)); bodyHtml.Replace("{pinyin}", escape(GetPinyinString(entry.GetPinyinForDisplay(true)))); } // Everything else: very full-fledged entry else { bodyHtml.Append(template4); bodyHtml.Replace("{hanzi1}", escape(hanzi1)); bodyHtml.Replace("{hanzi2}", escape(hanzi2)); bodyHtml.Replace("{pinyin}", escape(GetPinyinString(entry.GetPinyinForDisplay(true)))); } // In all but the first, simplest case, dealing with senses is the same if (mustDoSenses) { StringBuilder sbSenses = new StringBuilder(); foreach (CedictSense sense in entry.Senses) { string senseHtml = ""; if (!sense.Domain.EqualsPlainText("CL:")) { senseHtml += templateDiamond; senseHtml += " "; } senseHtml += getSenseHtmlPure(tprov, sense, script); senseHtml = templateSense.Replace("{sense}", senseHtml); sbSenses.Append(senseHtml); } bodyHtml.Replace("{senses}", sbSenses.ToString()); } // Assemble the whole HTML StringBuilder sb = new StringBuilder(); sb.Append(templateOuter); sb.Replace("{body}", bodyHtml.ToString()); // Purge new lines and tabs: this avoids extra spaces e.g. when pasting into Word sb.Replace("\r\n", ""); sb.Replace("\t", ""); // Done return sb.ToString(); }
/// <summary> /// Returns true of display font covers all Hanzi in entry; false otherwise. /// </summary> private bool areHanziCovered(CedictEntry entry) { // Simplified and traditional headword if (!areHanziCovered(entry.ChSimpl, entry.ChTrad)) return false; // Hanzi in hybrid text of senses for (int i = 0; i != entry.SenseCount; ++i) { CedictSense cs = entry.GetSenseAt(i); if (!areHanziCovered(cs.Domain)) return false; if (!areHanziCovered(cs.Equiv)) return false; if (!areHanziCovered(cs.Note)) return false; } // We're good to go. return true; }