跳转到内容

用户:A2569875-bot/Code/WikiTemplateArgUtil.cs

维基百科,自由的百科全书
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using DotNetWikiBot;

namespace WikiTemplateArgUtil
{
    public abstract class WikiTemplateBase : WikiEntity
    {
        public List<WikiTemplateArgBase> arg_list;
        public WikiText Name;
        public int start_pos;
        public int stop_pos;
        protected abstract string StartSymbol { get; }
        protected abstract string EndSymbol { get; }
        public override string genCode()
        {
            string gen_template = "";
            for (int i = 0; i < arg_list.Count; ++i)
            {
                gen_template += arg_list[i].genCode();
            }
            return StartSymbol + Name.genCode() + gen_template + EndSymbol;
        }
    }

    public abstract class WikiTemplateArgBase : WikiEntity
    {
        public WikiText Value;
        public abstract override string genCode();
        public static WikiTemplateArgBase Create(TreeNode<ParsingTreeNode> parsing_tree)
        {
            WikiTemplateArgBase temp = null;
            if (parsing_tree.Children.Count <= 2)
            {
                temp = new WikiTemplateValue(new WikiText(parsing_tree[1]));
            }else if (parsing_tree.Children.Count == 3)
            {
                temp = new WikiTemplateArg(new WikiText(parsing_tree[1]), new WikiText(new WikiFlatText("")));
            }
            else
            {
                temp = new WikiTemplateArg(new WikiText(parsing_tree[1]), new WikiText(parsing_tree[3]));
            }
            return temp;
        }
        public static WikiTemplateArgBase Create(string arg, string val)
        {
            return new WikiTemplateArg( new WikiText(new WikiFlatText(arg)), 
                                        new WikiText(new WikiFlatText(val)));
        }
    }

    public class WikiTemplateValue : WikiTemplateArgBase
    {
        public override string genCode()
        {
            return "|" + Value.genCode();
        }
        internal WikiTemplateValue(WikiText val)
        {
            Value = val;
        }
        public override string ToString()
        {
            return "(Arg) = " + Value;
        }
    }
    public class WikiTemplateArg : WikiTemplateArgBase
    {
        public WikiText Name;
        public override string genCode()
        {
            return "\n| " + Name.genCode() + " = " + Value.genCode();
        }
        internal WikiTemplateArg(WikiText arg, WikiText val)
        {
            Name = arg;
            Value = val;
        }
        public override string ToString()
        {
            return Name + " = " + Value;
        }
    }

}