示例#1
0
 private void GetTitle()
 {
     if (string.IsNullOrEmpty(title))
     {
         var ns = htmlDoc.DocumentNode.SelectNodes("//title");
         if (ns != null)
         {
             title     = ns.First().InnerText;
             title     = WebUtility.HtmlDecode(title);
             titleText = UtilWxg.GetMatchGroup(title, @"\>(\w+)", 1);
         }
     }
 }
示例#2
0
        public CaseFile(string htmlPath)
        {
            this.htmlDoc = new HtmlDocument();

            this.htmlDoc.Load(File.OpenRead(htmlPath), true);
            this.subFileName = UtilWxg.GetMatchGroup(htmlPath, subFilePathSetting, 1);

            FileInfo fi = new FileInfo(htmlPath);

            this.FileName = UtilWxg.GetMatchGroup(fi.Name, @"(.+)\.\w+$", 1);

            GetTitle();
        }
示例#3
0
        public void refresh(XPathRuleItem ruleItem, HtmlNode node)
        {
            Console.WriteLine("ruleItem:" + ruleItem.name);

            setEventKeyByAttr(ruleItem, node);
            Console.WriteLine("eventKey:" + this.eventKey);
            setEventNameByAttr(ruleItem, node);

            if (string.IsNullOrEmpty(this.eventName) || !Regex.IsMatch(eventName, @"\w+"))
            {
                if (!string.IsNullOrEmpty(this.eventKey))
                {
                    if (ruleItem.wordMap.ContainsKey(this.eventKey))
                    {
                        this.eventName = ruleItem.wordMap[this.eventKey];
                    }
                    else
                    {
                        this.eventName = this.eventKey;
                    }
                }
            }
            else if (Regex.IsMatch(this.eventName, ptwords))
            {
                this.eventName = UtilWxg.GetMatchGroup(this.eventName, ptwords, 1);
            }
            if (string.IsNullOrEmpty(this.eventName))
            {
                if (node.FirstChild != null)
                {
                    if (node.FirstChild.Name.Contains("#text"))
                    {
                        this.eventName = node.FirstChild.InnerText;
                    }
                }
            }
            else if (ruleItem.wordMap.ContainsKey(this.eventName))
            {
                this.eventName = ruleItem.wordMap[this.eventName];
            }
            if (Regex.IsMatch(this.eventName, @"\n|\r"))
            {
                this.eventName = Regex.Replace(this.eventName, @"\n|\r", "");
            }
            this.eventName = this.eventName.Trim();
            Console.WriteLine("eventName:" + this.eventName);
        }
示例#4
0
        private static void LoadYmlRules <T>(List <T> items, string ruleFolderPath, string cmd)
        {
            if (!Directory.Exists(ruleFolderPath))
            {
                return;
            }

            var deserializer = new Deserializer();

            foreach (var filepath in Directory.GetFiles(ruleFolderPath))
            {
                if (!Regex.IsMatch(filepath, ".(yml|yaml)$"))
                {
                    continue;
                }

                using (StreamReader reader = File.OpenText(filepath))
                {
                    string name = UtilWxg.GetMatchGroup(filepath, @"\\*(\w+)\.\w+", 1);

                    if (string.IsNullOrEmpty(cmd) || name.Equals(cmd))
                    {
                        T item = deserializer.Deserialize <T>(reader);
                        items.Add(item);

                        FieldInfo fi = item.GetType().GetField("name");
                        fi.SetValue(item, name);
                    }
                }
            }

            DirectoryInfo di = new DirectoryInfo(ruleFolderPath);

            foreach (var sdi in di.GetDirectories())
            {
                LoadYmlRules(items, sdi.FullName, cmd);
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            //args check
            //==============================================================
            if (args.Length < 2)
            {
                Console.WriteLine("TextConv [-p PATTERN] [-r REPLACEMENT] [-d srcfolder] [-f srcfile]");
                Console.WriteLine("TextConv [-c COMMAND_KEY] [-x XPATH] [-d srcfolder] [-f srcfile]");
                return;
            }
            string cmd = getValue("-c", args);

            string srcfolder = getValue("-d", args);

            if (string.IsNullOrEmpty(srcfolder))
            {
                srcfolder = Config.GetAppSettingValue("srcfolder");
            }
            resultFolder = UtilWxg.GetMatchGroup(srcfolder, @"(\w+)\\*$", 1);

            string srcFile = getValue("-f", args);

            if (string.IsNullOrEmpty(srcfolder) && string.IsNullOrEmpty(srcFile))
            {
                Console.WriteLine("App.config setting srcfolder is required.");
                return;
            }

            //==============================================================
            if (args.Contains("-x"))
            {
                List <XPathRuleItem> rules = new List <XPathRuleItem>();
                string ruleFolderPath      = Config.GetAppSettingValue("xpath.rule.yml");
                LoadYmlRules(rules, ruleFolderPath, cmd);
                HtmlParseFolder(srcfolder, rules);
                HtmlParseFile(srcFile, rules);
            }

            //==============================================================
            if (args.Contains("-c") || args.Contains("-p"))
            {
                List <ReplaceRule> repRules       = new List <ReplaceRule>();
                string             ruleFolderPath = Config.GetAppSettingValue("replace.rule.yml");
                LoadYmlRules(repRules, ruleFolderPath, cmd);

                ReplaceRuleItem ri = new ReplaceRuleItem();
                ri.pattern     = getValue("-p", args);
                ri.replacement = getValue("-r", args);
                if (!string.IsNullOrEmpty(ri.pattern))
                {
                    string content = getValue("-input", args);
                    if (!string.IsNullOrEmpty(content))
                    {
                        Console.WriteLine(ri.replaceText(content));
                    }
                    else
                    {
                        ReplaceRule rule = new ReplaceRule();
                        rule.rules.Add(ri);
                        repRules.Add(rule);
                    }
                }
                ReplaceFolder(srcfolder, repRules);
                ReplaceFile(srcfolder, repRules);
            }
        }