/// <summary> /// in the future it might be a worthy idea to include AvalonEdit.TextEditor.Document /// or at least the Editor so that we can get to the elements within /// to do our writing natively to the Editor. /// </summary> /// <param name="page"></param> /// <param name="justText"></param> /// <returns></returns> static public string HtmlArtifacts(this Post page, bool justText) { string output = string.Empty; // List<string> output = new List<string>(); // logging something for no apparent reason. // output.Add("created list"); // Read Html Document HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8; doc.OptionCheckSyntax = false; doc.OptionOutputAsXml = true; doc.OptionWriteEmptyNodes = true; // why are we buffering tagcontent? byte[] buffer = System.Text.Encoding.UTF8.GetBytes(page.TagContent()); using (MemoryStream instream = new MemoryStream(buffer, 0, buffer.Length, true)) { doc.Load(instream); XPathNavigator n = doc.CreateNavigator(); n.MoveToFirst(); output += n.Iterate(true); } doc = null; buffer = null; return(output); }
private static List <Choice> ParseChoices(string choicesHtml) { choicesHtml = System.Web.HttpUtility.HtmlDecode(choicesHtml); var choices = new List <Choice>(); /* * <div id="room-choices"><h2>You have 4 choices:</h2> * <ul class="choices"> * <li><a href="/story/choice.php?id=184898" title="184898,94419">Years later…</a></li> * <li><a href="/story/choice.php?id=184899" title="184899,94416">The Empire (Lies)</a></li> * <li><a href="/story/choice.php?id=184900" title="184900,94417">The Empire (Truth)</a></li> * <li><a href="/story/choice.php?id=184901" title="184901,94418">The Eternal Program (Hope)</a></li> * </ul> * </div> */ HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(new StringReader(choicesHtml)); var nav = doc.CreateNavigator(); var strExpression = "//a"; foreach (HtmlAgilityPack.HtmlNode link in doc.DocumentNode.SelectNodes(strExpression)) { string titleValue = link.GetAttributeValue("title", null); String[] titleSplit = titleValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); Choice choice = new Choice(titleSplit[0], titleSplit[1], link.InnerHtml); choices.Add(choice); } return(choices); }
private static String ParseSaveAndFixImages(string contents, string dirPath) { contents = System.Web.HttpUtility.HtmlDecode(contents); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(new StringReader(contents)); var nav = doc.CreateNavigator(); var strExpression = "//img"; HtmlAgilityPack.HtmlNodeCollection imgTags = doc.DocumentNode.SelectNodes(strExpression); if (imgTags != null) { foreach (HtmlAgilityPack.HtmlNode tag in imgTags) { if (tag.Attributes["src"] != null) { String imgPath = tag.Attributes["src"].Value; tag.Attributes["src"].Value = GetAndSaveImage(imgPath, dirPath); } } } string finalContents = null; using (StringWriter sw = new StringWriter()) { doc.Save(sw); finalContents = sw.ToString(); } return(finalContents); }
private static ConcurrentBag <Disciplina> LerDisciplinas(string urlCampus, string p, ClienteWeb wc) { var discs = new ConcurrentBag <Disciplina>(); wc = new ClienteWeb(); wc.Encoding = Encoding.UTF8; var newUrl = urlCampus.Substring(0, urlCampus.LastIndexOf("/") + 1) + p; string html; try { html = wc.DownloadString(newUrl); } catch (Exception) { return(LerDisciplinas(urlCampus, p, wc)); } var dom = new HtmlAgilityPack.HtmlDocument(); dom.LoadHtml(html); var navigator = dom.CreateNavigator(); var tabela = navigator.Select("//table[@class='FrameCinza']"); while (tabela.MoveNext()) { var trs = tabela.Current.Select(".//tr"); trs.MoveNext(); var f = new FilaDisciplinas(); while (trs.MoveNext()) { var td = trs.Current.Select(".//td[2]"); td.MoveNext(); // estamos no TD que queremos var url = td.Current.Select(".//a/@href"); url.MoveNext(); var nome = td.Current.Select(".//a/text()"); nome.MoveNext(); f.Put(Tuple.Create(urlCampus, url.Current.Value, discs, nome.Current.Value)); } } return(discs); }
private static EnvironmentConfig getConfigFromFile(FileInfo fileInfo) { EnvironmentConfig cfg = new EnvironmentConfig(); cfg.FileInfo = fileInfo; cfg.IsGuardServerAllowed = !System.Text.RegularExpressions.Regex.IsMatch(fileInfo.Name, "NoGS", System.Text.RegularExpressions.RegexOptions.IgnoreCase); cfg.Name = System.Text.RegularExpressions.Regex.Replace(fileInfo.Name, @"\.ttmd\.cfg", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); cfg.Name = System.Text.RegularExpressions.Regex.Replace(cfg.Name, @"\.NoGS", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); using (var reader = fileInfo.OpenRead()) { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(reader); var xpathNavi = doc.CreateNavigator(); var multicastNode = xpathNavi.SelectSingleNode("/ttmconfiguration[1]/multicastgroups[1]"); if (multicastNode != null && !String.IsNullOrEmpty(multicastNode.Value)) { cfg.Multicast = multicastNode.Value.Replace("> =", String.Empty).Trim(); } var daemonNode = xpathNavi.SelectSingleNode("/ttmconfiguration[1]/proxy[1]/daemon1[1]"); if (daemonNode != null && !String.IsNullOrEmpty(daemonNode.Value)) { var regex = new System.Text.RegularExpressions.Regex(@"\d+\.\d+\.\d+\.\d+", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline); var match = regex.Match(daemonNode.Value); if (match.Success) { cfg.RemoteDaemon = match.Value; cfg.Multicast = null; } } } return cfg; }
public static List <Departamento> LerDepartamentos(string urlCampus) { List <Departamento> deps = new List <Departamento>(); ClienteWeb wc = new ClienteWeb(); wc.Encoding = Encoding.UTF8; var html = wc.DownloadString(urlCampus); var dom = new HtmlAgilityPack.HtmlDocument(); dom.LoadHtml(html); var navigator = dom.CreateNavigator(); var tabela = navigator.Select("//table[@class='FrameCinza']"); List <Tuple <Departamento, string, string> > deptoTemp = new List <Tuple <Departamento, string, string> >(); while (tabela.MoveNext()) { var trs = tabela.Current.Select(".//tr"); trs.MoveNext(); while (trs.MoveNext()) { var td = trs.Current.Select(".//td[3]"); td.MoveNext(); // estamos no TD que queremos var url = td.Current.Select(".//a/@href"); url.MoveNext(); var nome = td.Current.Select(".//a/text()"); nome.MoveNext(); var dep = new Departamento(); dep.Nome = nome.Current.Value; deptoTemp.Add(Tuple.Create(dep, urlCampus, url.Current.Value)); } } /* deptoTemp.RemoveAll(n => * !@"Departamento de Administração * Departamento de Ciência da Computação * Departamento de Economia * Departamento de Engenharia Elétrica. * Departamento de Engenharia Mecânica * Departamento de Estatística * Departamento de Filosofia * Departamento de Matemática. * Departamento de Planejamento e Administração Departamento de Sociologia * Departamento de Teoria e Fundamentos * Depto de Ciências Contábeis e Atuariais * Direção da Faculdade de Tecnologia * Faculd. de Economia, Administração e Contabilidade * Faculdade de Ciência da Informação * Faculdade de Tecnologia * ".Contains(n.Item1.Nome.Trim()));*/ foreach (var depi in deptoTemp) { var dep = depi.Item1; Console.WriteLine("Lendo dpto. " + dep.Nome); dep.Disciplinas = LerDisciplinas(depi.Item2, depi.Item3, null); } var f = new FilaDisciplinas(); f.Put(null); return(deptoTemp.Select(n => n.Item1).ToList()); }
public static List <Turma> LerTurmas2(ClienteWeb wc, string newUrl) { var turmas = new List <Turma>(); string html; try { html = wc.DownloadString(newUrl); } catch (Exception) { return(LerTurmas2(wc, newUrl)); } var dom = new HtmlAgilityPack.HtmlDocument(); dom.LoadHtml(html); var navigator = dom.CreateNavigator(); var tabela = navigator.Select("//table[@class='framecinza']"); tabela.MoveNext(); tabela.MoveNext(); var trs = tabela.Current.Select(".//tr[@bgcolor='#FFFFFF']"); while (trs.MoveNext()) { if (!(trs.Current.Select(".//td").Count > 3)) { continue; } var nome = trs.Current.Select(".//td[1]/div/font/b/text()"); nome.MoveNext(); var turma = new Turma(); turma.Nome = nome.Current.Value; var vagas = trs.Current.Select(".//td[2]"); vagas.MoveNext(); string text = vagas.Current.Value; var rgx = new Regex("\\d+"); foreach (Match mt in rgx.Matches(text)) { if (turma.Total == null) { turma.Total = int.Parse(mt.ToString()); } else if (turma.Ocupadas == null) { turma.Ocupadas = int.Parse(mt.ToString()); } else if (turma.Disponiveis == null) { turma.Disponiveis = int.Parse(mt.ToString()); } } turma.Aulas = new List <Aula>(); var horas = trs.Current.Select(".//td[4]/div"); while (horas.MoveNext()) { var dictSemana = new Dictionary <string, DayOfWeek>() { { "Segunda", DayOfWeek.Monday }, { "Terça", DayOfWeek.Tuesday }, { "Quarta", DayOfWeek.Wednesday }, { "Quinta", DayOfWeek.Thursday }, { "Sexta", DayOfWeek.Friday }, { "Sábado", DayOfWeek.Saturday }, { "Domingo", DayOfWeek.Sunday }, }; var diaSemana = horas.Current.Select("./b"); diaSemana.MoveNext(); string diaText = diaSemana.Current.Value; // 1o font. com b dentro hora de inicio var horaInicio = horas.Current.Select("./font[1]/b/text()"); horaInicio.MoveNext(); string horaIniciStr = horaInicio.Current.Value; // 2o font. texto direto dentro hora de fim var horaFim = horas.Current.Select(".//font[2]/text()"); horaFim.MoveNext(); string horaFimStr = horaFim.Current.Value; turma.Aulas.Add(new Aula() { Dia = dictSemana[diaText], HoraFim = TimeSpan.Parse(horaFimStr), HoraInicio = TimeSpan.Parse(horaIniciStr) }); } turmas.Add(turma); } return(turmas); }
private static List<Choice> ParseChoices(string choicesHtml) { choicesHtml = System.Web.HttpUtility.HtmlDecode(choicesHtml); var choices = new List<Choice>(); /* * <div id="room-choices"><h2>You have 4 choices:</h2> * <ul class="choices"> * <li><a href="/story/choice.php?id=184898" title="184898,94419">Years later…</a></li> * <li><a href="/story/choice.php?id=184899" title="184899,94416">The Empire (Lies)</a></li> * <li><a href="/story/choice.php?id=184900" title="184900,94417">The Empire (Truth)</a></li> * <li><a href="/story/choice.php?id=184901" title="184901,94418">The Eternal Program (Hope)</a></li> * </ul> * </div> */ HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(new StringReader(choicesHtml)); var nav = doc.CreateNavigator(); var strExpression = "//a"; foreach (HtmlAgilityPack.HtmlNode link in doc.DocumentNode.SelectNodes(strExpression)) { string titleValue = link.GetAttributeValue("title", null); String[] titleSplit = titleValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); Choice choice = new Choice(titleSplit[0], titleSplit[1], link.InnerHtml); choices.Add(choice); } return choices; }
private static String ParseSaveAndFixImages(string contents, string dirPath) { contents = System.Web.HttpUtility.HtmlDecode(contents); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(new StringReader(contents)); var nav = doc.CreateNavigator(); var strExpression = "//img"; HtmlAgilityPack.HtmlNodeCollection imgTags = doc.DocumentNode.SelectNodes(strExpression); if (imgTags != null) { foreach (HtmlAgilityPack.HtmlNode tag in imgTags) { if (tag.Attributes["src"] != null) { String imgPath = tag.Attributes["src"].Value; tag.Attributes["src"].Value = GetAndSaveImage(imgPath, dirPath); } } } string finalContents = null; using (StringWriter sw = new StringWriter()) { doc.Save(sw); finalContents = sw.ToString(); } return finalContents; }
private void go_button_Click(object sender, EventArgs e) { ProgressDialog d = new ProgressDialog(); string character_path = character_box.Text; string macro_path = macro_box.Text; List<PowerInfo> powers = new List<PowerInfo>(); Dictionary<string, int> skills = new Dictionary<string, int>(); System.Text.RegularExpressions.Regex img_remover = new System.Text.RegularExpressions.Regex(@"<img.*?>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); // save the value of the checkbox m_use_compendium = compendium_check.Checked; d.AddTask((progress) => { progress.Update("Scanning character sheet..."); using (FileStream fs = File.OpenRead(character_path)) { XPathDocument doc = new XPathDocument(fs); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator power_iter, weapon_iter; // select all power nodes power_iter = nav.Select("/D20Character/CharacterSheet/PowerStats/Power"); // update the progress steps progress.Update(0, power_iter.Count); // iterate them while (power_iter.MoveNext()) { string name; string action_type; List<string> compendium_info = new List<string>(); PowerUsage usage; XPathNavigator power_url_nav = null; try { // read the basic stuff name = power_iter.Current.SelectSingleNode("@name").Value; usage = Util.EnumParse<PowerUsage>(power_iter.Current.SelectSingleNode("specific[@name = 'Power Usage']").Value.Replace("-", "")); action_type = power_iter.Current.SelectSingleNode("specific[@name = 'Action Type']").Value.Trim(); // get the url for the power in the compendium if (m_use_compendium) power_url_nav = nav.SelectSingleNode("//RulesElementTally/RulesElement[@name = \"" + name + "\"]/@url"); // ...and if we did that, pull down the power description if (power_url_nav != null) { HtmlAgilityPack.HtmlDocument scraper_doc = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlNodeNavigator scraper_result; XPathNodeIterator content_iter; XPathNavigator scraper_nav = null; // slurp try { using (Stream s = m_compendium.GetEntryByUrl(power_url_nav.Value)) { scraper_doc.Load(s, new UTF8Encoding()); scraper_nav = scraper_doc.CreateNavigator(); } } catch (Exception ex) { // if the user clicked cancel, stop attempting compendium stuff if (ex is ApplicationException && ex.Message.ToLowerInvariant().Contains("user refused")) m_use_compendium = false; else MessageBox.Show(ex.ToString(), "Compendium Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // if the web request succeeded... if (scraper_nav != null) { // select all the detail p tags content_iter = scraper_nav.Select("//div[@id = 'detail']/p"); // loop through them while (content_iter.MoveNext()) { string line; // cast to HtmlNodeNavigator scraper_result = content_iter.Current as HtmlAgilityPack.HtmlNodeNavigator; // skip publishing stuff if (scraper_result == null || scraper_result.CurrentNode.InnerHtml.ToLowerInvariant().Contains("published in")) continue; // grab the line, strip images, and remove [] stuff line = scraper_result.CurrentNode.InnerHtml; line = img_remover.Replace(line, "-"); line = line.Replace("[W]", "<i>W</i>"); // add the line compendium_info.Add(line); } } } // select weapons weapon_iter = power_iter.Current.Select("Weapon"); // some powers aren't attacks or don't use weapons if (weapon_iter.Count < 1) { powers.Add(new PowerInfo(name, compendium_info, WEAPON_NONE, usage, action_type)); } else { while (weapon_iter.MoveNext()) { PowerInfo power = new PowerInfo(name, compendium_info, weapon_iter.Current.SelectSingleNode("@name").Value, usage, action_type); // when there is more than one weapon, skip the Unarmed weapon if (power.Weapon == WEAPON_UNARMED && weapon_iter.Count > 1) continue; // read the attack info power.AttackBonus = weapon_iter.Current.SelectSingleNode("AttackBonus").ValueAsInt; power.AttackStat = weapon_iter.Current.SelectSingleNode("AttackStat").Value.Trim(); power.DamageExpression = weapon_iter.Current.SelectSingleNode("Damage").Value.Trim(); power.Defense = weapon_iter.Current.SelectSingleNode("Defense").Value.Trim(); // all powers list an Unarmed weapon, even if they don't include an attack, set these to WEAPON_NONE if (power.Defense == "") power.Weapon = WEAPON_NONE; // add it to the list powers.Add(power); } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // wait briefly to avoid spamming compendium if (m_use_compendium) System.Threading.Thread.Sleep(200); // advance progress progress.Advance(); } // detect skills from the RulesElementTally power_iter = nav.Select("//RulesElementTally/RulesElement[@type = 'Skill']/@name"); // read the value for each one while (power_iter.MoveNext()) { string name = power_iter.Current.Value; int value; XPathNavigator value_node = nav.SelectSingleNode("/D20Character/CharacterSheet/StatBlock/Stat[@name = '" + name + "']/@value"); if (value_node != null && int.TryParse(value_node.Value, out value)) skills[name] = value; } } }); d.AddTask((progress) => { progress.Update("Writing macros..."); using (FileStream fs = File.Open(macro_path, FileMode.Create, FileAccess.Write)) { XmlWriterSettings settings; settings = new XmlWriterSettings(); settings.Encoding = new System.Text.ASCIIEncoding(); settings.Indent = true; settings.IndentChars = " "; settings.OmitXmlDeclaration = false; using (XmlWriter w = XmlWriter.Create(fs, settings)) { w.WriteStartDocument(); w.WriteStartElement("list"); foreach (PowerInfo power in powers) { StringBuilder command = new StringBuilder(); string text, background; // skip basic attacks with no weapons if (IsPowerBasicAttack(power.Name) && power.Weapon == WEAPON_NONE) continue; // begin macro definition w.WriteStartElement(MACRO_ELEMENT_NAME); w.WriteElementString("saveLocation", "Token"); w.WriteElementString("label", power.Name); w.WriteElementString("autoExecute", "true"); w.WriteElementString("sortby", GetSortPriority(power)); // set colors based on usage limits if (GetColors(power.Usage, out text, out background)) { w.WriteElementString("fontColorKey", text); w.WriteElementString("colorKey", background); } // start with the table and header command.AppendLine("<table border=\"0\">"); command.AppendFormat("<tr bgcolor=\"{0}\" style=\"color: white;\">", background); command.AppendLine(); command.AppendFormat("<th align=\"left\" style=\"font-size: 1.1em; padding: 2px 4px;\">{0}</th>", power.Name); command.AppendLine(); command.AppendFormat("<td align=\"right\" valign=\"middle\" style=\"padding: 2px 4px;\"><i>{0}</i></td></tr>", power.Weapon == WEAPON_NONE ? "" : power.Weapon); command.AppendLine(); command.AppendLine("</tr>"); // write compendium info foreach (string line in power.CompendiumInfo) command.AppendLine("<tr><td colspan=\"2\">" + line + "</td></tr>"); // when we have a weapon, put the macro in the weapon's group and include the rolls if (power.Weapon != WEAPON_NONE) { w.WriteElementString("group", "Attacks - " + power.Weapon); command.AppendFormat("<tr><td nowrap><b>[1d20+{0}]</b></td><td><b>vs. {1}</b></td></tr>", power.AttackBonus, power.Defense); command.AppendLine(); command.AppendFormat("<tr><td nowrap>[{0}]</td><td>damage</td></tr>", power.DamageExpression); command.AppendLine(); } // finish up the command command.AppendLine("</table>"); // write the command and end element w.WriteElementString("command", command.ToString()); w.WriteEndElement(); // MACRO_ELEMENT_NAME } // write skill check macros foreach (KeyValuePair<string, int> skill in skills) { // begin macro definition w.WriteStartElement(MACRO_ELEMENT_NAME); w.WriteElementString("saveLocation", "Token"); w.WriteElementString("label", skill.Key); w.WriteElementString("autoExecute", "true"); w.WriteElementString("group", "Skills"); w.WriteElementString("command", string.Format("<p><b>{0} Check</b> [1d20+{1}]</p>", skill.Key, skill.Value)); w.WriteEndElement(); // MACRO_ELEMENT_NAME } w.WriteEndDocument(); } } }); try { d.Show(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }