示例#1
0
        public GlossyString GlossyFromInline(InlineString inline, bool ignore_show_specials = false)
        {
            var g = new GlossyString();

            foreach (var rwp in inline.RunsWithProperties)
            {
                Gloss gloss;
                var   prop = rwp.Property;
                if (ShowChanges)
                {
                    switch (prop)
                    {
                    case InlineProperty.None: gloss = Gloss.None; break;

                    case InlineProperty.Ins: gloss = Gloss.INS; break;

                    case InlineProperty.Del: gloss = Gloss.DEL; break;

                    case InlineProperty.Emp: gloss = Gloss.EMP; break;

                    default:
                        throw new ApplicationException("internal error: unknown InlineProperty value in RWP.");
                    }
                }
                else
                {
                    switch (prop)
                    {
                    case InlineProperty.None: gloss = Gloss.None; break;

                    case InlineProperty.Ins: gloss = Gloss.None; break;

                    case InlineProperty.Del: continue;     // XXX

                    case InlineProperty.Emp: gloss = Gloss.EMP; break;

                    default:
                        throw new ApplicationException("internal error: unknown InlineProperty value in RWP.");
                    }
                }

                var run = rwp.Run;
                if (run is InlineText)
                {
                    var str = (run as InlineText).Text;
                    if (!ShowSpecials || ignore_show_specials)
                    {
                        g.Append(str, gloss);
                    }
                    else
                    {
                        int p = 0;
                        for (int q = 0; q < str.Length; q++)
                        {
#if true
                            // The version with SpecialCharChecker.
                            var c = str[q];
                            if (SpecialCharChecker[c % SpecialCharChecker.Length] == c)
                            {
                                if (q > p)
                                {
                                    g.Append(str.Substring(p, q - p), Gloss.None);
                                }
                                g.Append(SpecialCharMapRaw[c], gloss | Gloss.SYM);
                                g.Append(SpecialCharMapAlt[c], gloss | Gloss.ALT);
                                p = q + 1;
                            }
#else
                            // The version without SpecialCharChecker.
                            string special;
                            if (SpecialCharMap.TryGetValue(str[q], out special))
                            {
                                if (q > p)
                                {
                                    g.Append(str.Substring(p, q - p), Gloss.None);
                                }
                                g.Append(SpecialCharMapRaw[c], Gloss.SYM);
                                g.Append(SpecialCharMapAlt[c], Gloss.ALT);
                                p = q + 1;
                            }
#endif
                        }
                        if (p < str.Length)
                        {
                            g.Append(str.Substring(p), gloss);
                        }
                    }
                }
                else if (run is InlineTag)
                {
                    var tag  = (InlineTag)run;
                    var text = tag.ToString((InlineString.Render)TagShowing);
                    if (!string.IsNullOrEmpty(text))
                    {
                        g.Append(text, gloss | Gloss.TAG);
                    }
                }
                else
                {
                    throw new ApplicationException("internal error: unknown type in RWP.Run");
                }
            }
            g.Frozen = true;
            return(g);
        }
示例#2
0
        public GlossyString GlossyFromInline(InlineString inline, bool ignore_show_specials = false)
        {
            var g = new GlossyString();

            foreach (var run in inline.RunsWithProperties.Where(rwp => rwp.Property != InlineProperty.Del).Select(rwp => rwp.Run))
            {
                if (run is InlineText)
                {
                    var str = (run as InlineText).Text;
                    if (!ShowSpecials || ignore_show_specials)
                    {
                        g.Append(str, Gloss.None);
                    }
                    else
                    {
                        int p = 0;
                        for (int q = 0; q < str.Length; q++)
                        {
#if true
                            // The version with SpecialCharChecker.
                            var c = str[q];
                            if (SpecialCharChecker[c % SpecialCharChecker.Length] == c)
                            {
                                if (q > p)
                                {
                                    g.Append(str.Substring(p, q - p), Gloss.None);
                                }
                                g.Append(SpecialCharMapRaw[c], Gloss.SYM);
                                g.Append(SpecialCharMapAlt[c], Gloss.ALT);
                                p = q + 1;
                            }
#else
                            // The version without SpecialCharChecker.
                            string special;
                            if (SpecialCharMap.TryGetValue(str[q], out special))
                            {
                                if (q > p)
                                {
                                    g.Append(str.Substring(p, q - p), Gloss.None);
                                }
                                g.Append(SpecialCharMapRaw[c], Gloss.SYM);
                                g.Append(SpecialCharMapAlt[c], Gloss.ALT);
                                p = q + 1;
                            }
#endif
                        }
                        if (p < str.Length)
                        {
                            g.Append(str.Substring(p), Gloss.None);
                        }
                    }
                }
                else if (run is InlineTag)
                {
                    var tag = (InlineTag)run;
                    switch (ShowTag)
                    {
                    case TagShowing.None:
                        break;

                    case TagShowing.Name:
                        g.Append(BuildTagString(tag, tag.Number.ToString()), Gloss.TAG);
                        break;

                    case TagShowing.Disp:
                        g.Append(Enclose(tag.Display) ?? BuildTagString(tag, tag.Name), Gloss.TAG);
                        break;

                    case TagShowing.Code:
                        g.Append(tag.Code ?? BuildTagString(tag, "*"), Gloss.TAG);
                        break;

                    default:
                        throw new ApplicationException("internal error");
                    }
                }
                else
                {
                    throw new ApplicationException("internal error");
                }
            }
            g.Frozen = true;
            return(g);
        }