FlexWiki中文支持

Title
Summary

FlexWiki的Topic采用Pascal单词形式,对中文的Topic无法自动进行处理,我对其进行了一些扩展以支持中文主题,

一. 在什么地方切入到FlexWiki的格式化处理中?

FlexWiki的设计并没有提供很好的扩展点,为了避免对原有的格式处理造成影响,采用在其对Topic内容格式化完成后进行切入,

 string formattedBody = TheFederation.GetTopicFormattedContent(topic, diffVersion);
 // 在此处切入扩展处理程序, FormatterExtension为自定义的格式化扩展程序.
 if (TheFederation.FormatterExtension != null)
        formattedBody = TheFederation.FormatterExtension.Format(TheFederation,  
                TheLinkMaker, topic, formattedBody);

二. 如何切入,以利于扩展其它格式的处理或移除?

显然,通过配置文件是最灵活的扩展设置方式,首先定义一个格式化扩展处理接口

 public interface IFormatterExtension
 {
        string Format(Federation federation, LinkMaker linkMaker, 
                AbsoluteTopicName topic, string body);
 }

然后在NamespaceMap.xml中加入以下定义

 <FormatterExtension>FlexWiki.Extensions.ChineseFormatterExtension, 
                FlexWiki.Extensions</FormatterExtension>

配置的处理程序就不列出了,请查看Federation.cs和FederationConfiguration.cs中的代码。

三. 如何给现有中文Topic加上主题链接?

创建一个中文格式扩展类ChineseFormatterExtension, 它实现了上面的扩展接口。

1. 取出所有Namespace的Topic.

 topics = new Hashtable();
 // 遍历所有Namespace
 foreach (ContentBase cb in federation.AllNamespaces)
 {
        // 遍历所有Topic
        IEnumerator em = cb.AllTopics(false).GetEnumerator();
        while (em.MoveNext())
        {
                AbsoluteTopicName tp = (AbsoluteTopicName)em.Current;
                if (tp.Name.Equals("_ContentBaseDefinition") 
                   || tp.Name.Equals("_NormalBorders"))
                        continue;
                if (!topics.ContainsKey(tp.Name))
                        topics[tp.Name] = new ExTopic(tp, true);//true标识这是一个存在的主题。
        }
 }

2. 给没有链接的Topic加上链接

 string content = body;
 string tooltips = "";


 // 遍历所有Topic, 给无链接的主题加上链接.
 foreach (string key in topics.Keys)
 {
        // 匹配有链接和无链接的主题.
        string pattern = string.Format("(<a.*?>.*?{0}.*?</a>)|(?<link>{1})", key, key);
        Regex r = new Regex(pattern, RegexOptions.IgnoreCase|RegexOptions.Compiled);
        ArrayList positions = new ArrayList();
        for (Match m = r.Match(content); m.Success; m = m.NextMatch()) 
        {
                int index = m.Groups[2].Index;
                if (index > 0)
                {
                        // 记录无链接的主题的位置和长度.
                        int i = content.LastIndexOf(">", index);
                        if (i == -1 || i > content.LastIndexOf("<", index))
                                positions.Add(new Position(index, m.Groups[2].Length));
                }
        }


        string tooltip;
        // 取得主题的链接和提示文本。
        string link = GetTopicLink(federation, linkMaker, (ExTopic)topics[key], out tooltip);
        StringBuilder sb = new StringBuilder(content);
        for (int i=positions.Count-1; i>=0; i--)
        {
                // 替换主题
                Position pos = (Position)positions[i];
                sb.Remove(pos.Index, pos.Length);
                sb.Insert(pos.Index, link);
        }
        content = sb.ToString();
        tooltips += tooltip;
 }
 其中: Position记录主题在正文中的位置信息, ExTopic包装TopicName对象,记录Topic是否是存在的。

三. 如何做一个能创建的中文Topic?

因为中文无法像Pascal形式的单词那样被识别,那么我们就通过一个文件来定义它们,

在NamespaceMap.xml相同的文件夹下创建一个cntopics.dat文件,其中每行定义一个主题,

格式为Namespace.TopicName,如果省略Namespace,则以默认空间处理。例如;

  Framework.程序框架
  中文测试

处理程序如下:

 FileInfo fi = new FileInfo(federation.FederationNamespaceMapFilename);
 string cntopics = fi.DirectoryName + "//cntopics.dat";
 if (File.Exists(cntopics))
 {
        StreamReader sr = new StreamReader(cntopics, Encoding.GetEncoding("GB2312"));
        string each = null;
        while ((each = sr.ReadLine()) != null)
        {
                if (each.Trim().Length == 0)
                        continue;


                if (each.IndexOf('.') == -1)
                        each = federation.DefaultNamespace + "." + each;


                // 创建一个主题.
                AbsoluteTopicName tp = new AbsoluteTopicName(each);
                if (!topics.ContainsKey(tp.Name))
                        topics[tp.Name] = new ExTopic(tp, false);//false标识这一个要创建的Topic。
        }
        sr.Close();
 }

FlexWiki1.8中文主题支持版下载 FlexWikiCore-1.8.0.1696-cn-0.1.zip

FlexWiki中文Topic支持 FlexWiki中文支持
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值