コード例 #1
0
ファイル: ResultsCtxtControl.cs プロジェクト: smartree/Zydeo
        /// <summary>
        /// Gets display strings by combining entry, sense index, and localized strings.
        /// </summary>
        private void getDisplayStrings(ITextProvider tprov, int senseIx,
                                       out string fullFormatted, out string fullCedict,
                                       out string hanzi1, out string hanzi2, out string pinyin, out string sense)
        {
            fullFormatted = tprov.GetString("CtxtCopyEntryFormatted");
            fullCedict    = tprov.GetString("CtxtCopyEntryCedict");
            pinyin        = tprov.GetString("CtxtCopyPinyin");
            string pinyinVal = CedictFormatter.GetPinyinString(entry.GetPinyinForDisplay(true), Magic.CtxtMenuMaxSyllableLength);

            pinyin = string.Format(pinyin, pinyinVal);
            sense  = null;
            hanzi1 = null;
            hanzi2 = null;
            if (script == SearchScript.Simplified || script == SearchScript.Traditional || entry.ChSimpl == entry.ChTrad)
            {
                hanzi1 = tprov.GetString("CtxtCopyHanzi");
                string hanzi1Val = script == SearchScript.Traditional ? entry.ChTrad : entry.ChSimpl;
                hanzi1Val = ellipse(hanzi1Val, Magic.CtxtMenuMaxSyllableLength);
                hanzi1    = string.Format(hanzi1, hanzi1Val);
            }
            else
            {
                hanzi1 = tprov.GetString("CtxtCopySimplified");
                string hanzi1Val = ellipse(entry.ChSimpl, Magic.CtxtMenuMaxSyllableLength);
                hanzi1 = string.Format(hanzi1, hanzi1Val);
                hanzi2 = tprov.GetString("CtxtCopyTraditional");
                string hanzi2Val = ellipse(entry.ChTrad, Magic.CtxtMenuMaxSyllableLength);
                hanzi2 = string.Format(hanzi2, hanzi2Val);
            }
            if (senseIx != -1)
            {
                sense = tprov.GetString("CtxtCopySense");
                string senseVal = getSense(senseIx);
                senseVal = ellipse(senseVal, Magic.CtxtMenuMaxSenseLength);
                sense    = string.Format(sense, senseVal);
            }
        }
コード例 #2
0
        /// <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());
        }