public void ConvertHtmlTest() { var converter = new HtmlToText(); const string headerText = "Header 1"; const string ulListItemText = "First bulleted item in the list."; const string olListItemText = "First item in the list."; var htmlContent = string.Format(@" <!DOCTYPE html> <html lang=""en"" xmlns=""http://www.w3.org/1999/xhtml""> <head> <meta charset=""utf-8"" /> <title></title> </head> <body> <p style=""margin-left: 40px""> {0}</p> <p> New paragraph. Plain text.</p> <p> a long sentence a long sentence a long sentence a long sentence a long sentence a long sentence a long sentence a long sentence a long sentence a long sentence a long sentence. </p> <div> <ul> <li>{1}</li> <li>Second bulleted item in the list.</li> </ul> <ol> <li>{2}</li> <li>Second item in the list.</li> </ol> </div> </body> </html>", headerText, ulListItemText, olListItemText); var plainText = converter.ConvertHtml(htmlContent); var paragrapthList = plainText.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).Where(p => !string.IsNullOrEmpty(p)).ToArray(); // New line after closing paragraph tag </p> Assert.IsTrue(paragrapthList.Contains(headerText)); // Each list item is on separate line. // ul list item should be started with "-" character. Assert.IsTrue(paragrapthList.Contains("- " + ulListItemText)); // Each list item is on separate line. Assert.IsTrue(paragrapthList.Contains(olListItemText)); }
/// <summary> /// 查询新闻列表 /// </summary> public ResponseModel GetNewsList(Expression <Func <News, bool> > where, int NewsClassifyId, int topCount) { var listAll = _db.News.Include("NewsClassify").Include("NewsComment"); var list = new List <News>(); if (NewsClassifyId == 0) //所有球队 { list = listAll.Where(s => s.NewsClassify.ParentId == 0 && s.NewsClassify.ClassifyType == 0).OrderByDescending(c => c.PublishDate).Take(topCount).ToList(); } else { var NewsClassifylst = _db.NewsClassify.Find(NewsClassifyId); if (NewsClassifylst.ParentId == 0 && NewsClassifylst.ClassifyType == 1) { var newsIds = _db.NewsClassify.Where(s => s.ParentId == NewsClassifyId).Select(c => c.Id); list = listAll.Where(s => newsIds.Contains(s.NewsClassifyId)).OrderByDescending(c => c.PublishDate).Take(topCount).ToList(); } else { list = listAll.Where(s => s.NewsClassifyId == NewsClassifyId).OrderByDescending(c => c.PublishDate).Take(topCount).ToList(); } } var response = new ResponseModel { code = 200, result = "新闻列表获取成功" }; response.data = new List <NewsModel>(); HtmlToText convert = new HtmlToText(); foreach (var news in list) { response.data.Add(new NewsModel { Id = news.Id, ClassifyName = news.NewsClassify.Name, Title = news.Title, Image = news.Image == null ? convert.ConvertImgSrc(news.Contents) : news.Image, Contents = convert.Convert(news.Contents).Length > 50 ? $"{convert.Convert(news.Contents).Substring(0, 50)}..." : convert.Convert(news.Contents), PublishDate = news.PublishDate.ToString("yyyy-MM-dd"), CommentCount = news.NewsComment.Count(), LoveCount = news.NewsComment.Sum(s => s.Love), Remark = news.Remark }); } return(response); }
public void MustProcessHtmlWithMultilineString() { var html = @"<html><head> <meta http-equiv=""content-type"" content=""text/html; charset=utf-8""></head> <body dir=""auto""> <div>Boa tarde,</div><div><br></div> <div>Gostaria de saber valores em caso de alteracao do voo de volta para quinta feira final do dia.</div> <div>Voo direto para sdu ou galeao.</div> <div>Favor sinalizar voo a partir das 18h com menor valor</div> <div><br></div> <div>Obrigada<br><br><div>Sent from my iPhone</div></div><div><br>"; var converter = new HtmlToText(html); var result = converter.GetText(); Assert.Contains("\r\n", result); }
// Convert downloaded HTML pages to Text file public static void ProcessFile(string path) { HtmlToText htt = new HtmlToText(); Console.WriteLine("Processed file '{0}'.", path); string s = htt.Convert(path); string pathString = @"c:\files\CryptoCrawler\Text\"; string fileName = "NewFile.txt"; StreamWriter sw = new StreamWriter(NextAvailableFilename(pathString + fileName)); sw.Write(s); sw.Flush(); sw.Close(); //Get Converted Text files ProcessTextDirectory(pathString); }
public string ToPlainText() { var txt = ""; // txt += title + Environment.NewLine; txt += description + Environment.NewLine; if (references != null) { foreach (var reference in references) { txt += HtmlToText.ConvertHtml(reference.content) + Environment.NewLine; } } return(txt); }
public JsonResult Index(string keyword) { HtmlToText convert = new HtmlToText(); int ReportId = 0; string s = keyword; var searchResultList = new List <SearchResultViewModel>(); int result; if (int.TryParse(s, out result)) { ReportId = Convert.ToInt32(keyword); } else { } IQueryable <Report> Reports = _applicationDbContext.Reports.Include(c => c.Category).Include(t => t.Topic).Include(ci => ci.City).Include(st => st.State); if (!String.IsNullOrEmpty(keyword)) { Reports = Reports .Where(d => d.CompanyorIndividual.ToLower().Contains(keyword) || d.ReportId == ReportId || d.ReportText.ToLower().Contains(keyword) || d.Website.ToLower().Contains(keyword) || d.Address.ToLower().Contains(keyword) || d.Category.Name.ToLower().Contains(keyword)).OrderByDescending(d => d.DateCreated); } foreach (var report in Reports) { SearchResultViewModel model = new SearchResultViewModel(); string title = report.CompanyorIndividual + ":" + convert.Convert(report.ReportText.Substring(0, 90)); string titleC = Regex.Replace(title, "[^A-Za-z0-9]", "-"); model.iD = Guid.NewGuid().ToString(); model.page = report.ReportId; model.title = titleC; model.company = report.CompanyorIndividual; model.Topic = report.Topic.Name; model.Category = report.Category.Name; searchResultList.Add(model); } return(Json(searchResultList, JsonRequestBehavior.AllowGet)); }
public void AutoAdjust() { if (Title.IsNullOrEmptyOrWhiteSpace()) { HtmlToText convert = new HtmlToText(); string _text = convert.Convert(this.Content); Title = _text.GetSentences().FirstOrDefault(); } if (Title.ToLower().Contains("[daily]")) { Repeat = ScheduleType.Daily; } if (Title.ToLower().Contains("[weekly]")) { Repeat = ScheduleType.Weekly; } if (Title.ToLower().Contains("[monthly]")) { Repeat = ScheduleType.Monthly; } if (Title.ToLower().Contains("[quaterly]")) { Repeat = ScheduleType.Quaterly; } if (Title.ToLower().Contains("[yearly]")) { Repeat = ScheduleType.Yearly; } //AutoSetTime this.AutoSetTimes(); //HasTags string[] _hasTags = Content.GetHasTags(); if (_hasTags != null && _hasTags.Count() > 0) { foreach (string _hasTag in _hasTags) { HasTags.Add(_hasTag); } } //Links //Links = this.Content.GetUrls().ToList(); }
/// <summary> /// Atualiza os dados pel Web Api /// Ou Joga os dados para a tela /// Apenas o status que altera /// </summary> /// <param name="sender">object</param> /// <param name="e">GridViewCommandEventArgs</param> protected void grdToDo_RowCommand(object sender, GridViewCommandEventArgs e) { //clicar no botão feito (já foi realizado essa atividade) if (e.CommandName == "Feito") { int index = int.Parse((string)e.CommandArgument); string chave = grdToDo.DataKeys[index]?["IdToDo"].ToString(); //preenche os dados var atividades = new AtividadesToDo() { NomeTodo = HtmlToText.StripHtml(grdToDo.Rows[index].Cells[1].Text), CompletoTodo = 1, IdTodo = int.Parse(chave) }; //atualiza os dados preenchidos da classe Atividades no Web Api HttpResponseMessage response = WebApiController.AtualizarDadosWebApi(chave, atividades); if (!response.IsSuccessStatusCode) { Response.Write(response.StatusCode + " - " + response.ReasonPhrase); } else { Response.Redirect("Default?guid=" + Guid.NewGuid() + "&id=AtualizadoSucesso"); } } //click no botão editar dentro do grid else if (e.CommandName == "Editar") { int index = int.Parse((string)e.CommandArgument); string chave = grdToDo.DataKeys[index]?["IdToDo"].ToString(); if (chave != null) { hdIdToDo.Value = chave; } txtTituloToDo.Text = HtmlToText.StripHtml(grdToDo.Rows[index].Cells[1].Text); btnEditar.Visible = true; btnEnviar.Visible = false; } }
public string[] Top10ContentWords(string url) { var proxy = new Web2StringProxy.ServiceClient(); var html = proxy.GetWebContent(url); proxy.Close(); var htmlToText = new HtmlToText(); var content = htmlToText.ConvertHtml(html); var contentWords = Regex.Split(content, @"[\W\d]+").Where(c => !String.IsNullOrWhiteSpace(c) && !_stopWords.Contains(c)); var topTenContentWords = (from w in contentWords group w by w into g let count = g.Count() orderby count descending select g.Key).Take(10); return(topTenContentWords.ToArray()); }
private void UpdateText() { if (Control == null || Element == null) { return; } var isHtml = HtmlLabel.GetIsHtml(Element); if (!isHtml) { return; } var plainText = HtmlToText.ConvertHtml(Element.Text); Control.Text = plainText; }
/// <summary> /// The DownloadArticleContent /// </summary> /// <param name="filters">The <see cref="string"/></param> public void DownloadArticleContent(string select, string[] filters) { var w = new HtmlAgilityPack.HtmlWeb(); HtmlDocument doc = new HtmlDocument(); if (Links.Count > 0) { try { HttpDownloader downloader = new HttpDownloader(Links[0].Uri.ToString(), null, Configuration.UserAgent); doc.LoadHtml(downloader.GetPage()); } catch (Exception ex) { doc = null; this.DisplayText = this.DisplayLine + " ERROR."; logger.Error($"Could not load {Links[0].Uri}"); logger.Error(ex); } } if (doc == null) { return; } HtmlToText conv = new HtmlToText() { Select = select, Filters = filters?.ToList(), LinkStartFrom = this.Links.Count }; Collection <Uri> links = new Collection <Uri>(); Collection <Uri> images = new Collection <Uri>(); var resultString = conv.ConvertHtml(doc.DocumentNode.OuterHtml, Links[0].Uri, out links, out images); //remove multiple lines from article content. It makes text more condensed. var cleanedContent = Regex.Replace(resultString, @"^\s+$[\r\n]*", "\r\n", RegexOptions.Multiline); ExternalLinks = links; ImageLinks = images; ArticleContent = cleanedContent; IsLoaded = true; Save(); }
private static object FromHtmlToText(ICoreItem coreItem, BodyWriteConfiguration configuration, Stream bodyStream, bool createWriter) { Stream stream = null; object obj = null; try { stream = new BodyCharsetDetectionStream(bodyStream, null, coreItem, BodyStreamFormat.Text, ConvertUtils.UnicodeCharset, configuration.TargetCharset, configuration.TargetCharsetFlags, null, false); HtmlToText htmlToText = new HtmlToText(); htmlToText.InputEncoding = configuration.SourceEncoding; htmlToText.OutputEncoding = ConvertUtils.UnicodeEncoding; htmlToText.DetectEncodingFromMetaTag = false; htmlToText.Header = configuration.InjectPrefix; htmlToText.Footer = configuration.InjectSuffix; htmlToText.HeaderFooterFormat = configuration.InjectionHeaderFooterFormat; if (configuration.ImageRenderingCallback != null) { TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToText, configuration.ImageRenderingCallback); } TextConverter converter = htmlToText; if (configuration.FilterHtml || configuration.InternalHtmlTagCallback != null) { stream = new ConverterStream(stream, htmlToText, ConverterStreamAccess.Write); converter = new HtmlToHtml { InputEncoding = configuration.SourceEncoding, OutputEncoding = configuration.SourceEncoding, DetectEncodingFromMetaTag = false, FilterHtml = configuration.FilterHtml, HtmlTagCallback = configuration.InternalHtmlTagCallback }; } obj = BodyWriteDelegates.GetConverterStreamOrWriter(stream, converter, createWriter); } finally { if (obj == null && stream != null) { BodyWriteDelegates.SafeDisposeStream(stream); } } return(obj); }
private static object FromHtmlToText(ICoreItem coreItem, BodyReadConfiguration configuration, Stream bodyStream, bool createReader) { HtmlToText htmlToText = new HtmlToText(); htmlToText.InputEncoding = ConvertUtils.GetItemMimeCharset(coreItem.PropertyBag).GetEncoding(); htmlToText.OutputEncoding = configuration.Encoding; htmlToText.DetectEncodingFromMetaTag = false; htmlToText.Header = configuration.InjectPrefix; htmlToText.Footer = configuration.InjectSuffix; htmlToText.HeaderFooterFormat = configuration.InjectionHeaderFooterFormat; htmlToText.ShouldUseNarrowGapForPTagHtmlToTextConversion = configuration.ShouldUseNarrowGapForPTagHtmlToTextConversion; htmlToText.OutputAnchorLinks = configuration.ShouldOutputAnchorLinks; htmlToText.OutputImageLinks = configuration.ShouldOutputImageLinks; if (configuration.ImageRenderingCallback != null) { TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToText, configuration.ImageRenderingCallback); } return(BodyReadDelegates.GetTextStreamOrReader(bodyStream, htmlToText, createReader)); }
public SmartText(string text) { Input = text; if (Input.IsHtmlString()) { Text = new HtmlToText().Convert(Input); } else { Text = Input; } Text = HttpUtility.HtmlDecode(Text); Text = Text.StripHtml(); Text = Text.ToStandard(); if (!Text.IsNullOrEmptyOrWhiteSpace()) { try { GetWords(); CountOfWords = Words.Count; GetEmails(); GetUrls(); GetLines(); KeyWords = GetVnKeywords(); Separators = Text.ExtractSeparators(); Sentences = Text.GetSentences(); Numbers = Text.GetNumbers(); YouTubeUrls = Text.GetYouTubeUrls(); GoogleDriveUrls = Text.GetGoogleDriveUrls(); //if (Words != null && Words.Count() > 0) //{ // FirstWord = Words[0]; // LastWord = Words[Words.Count - 1]; //} } catch (Exception ex) { HasError = true; throw new Exception("Can't convert this text to SmartText" + ex.Message); } } }
private void UpdateText() { if (Control == null || Element == null) { return; } if (string.IsNullOrEmpty(Control.Text)) { return; } var isHtml = HtmlLabel.GetIsHtml(Element); if (!isHtml) { return; } var removeTags = HtmlLabel.GetRemoveHtmlTags(Element); var text = removeTags ? HtmlToText.ConvertHtml(Control.Text) : Element.Text; Control.MovementMethod = LinkMovementMethod.Instance; var helper = new LabelRendererHelper(Element, text); var value = helper.ToString(); var html = Build.VERSION.SdkInt >= BuildVersionCodes.N ? Html.FromHtml(value, FromHtmlOptions.ModeCompact, null, new ListTagHandler()) : #pragma warning disable CS0618 // Il tipo o il membro è obsoleto Html.FromHtml(value, null, new ListTagHandler()); #pragma warning restore CS0618 // Il tipo o il membro è obsoleto Control.SetIncludeFontPadding(false); html = RemoveLastChar(html); Control.SetText(html, TextView.BufferType.Spannable); }
public async Task <BookFilesInfo> GetBookContent(BookInfo book) { if (!_lookup.ContainsKey(book.Id)) { return(null); } BookFilesInfo content = _lookup[book.Id]; var tasks = content.FileIds.Select(async id => await ReadBookFile(book, id)); BookFileInfo[] files = await Task.WhenAll(tasks); var html = new HtmlToText(); book.Description = html.Convert(book.Description); var contentWithBytes = new BookFilesInfo(book, files); return(contentWithBytes); }
public void MustProcessHtmlWithStyleTag() { var html = @" <html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:w=""urn:schemas-microsoft-com:office:word"" xmlns:m=""http://schemas.microsoft.com/office/2004/12/omml"" xmlns=""http://www.w3.org/TR/REC-html40""> <head> <style> * {behavior:url(#default#VML);} </style> </head> <body lang=PT-BR link=""#0563C1"" vlink=""#954F72""> <div class=WordSection1> <p class=MsoNormal>Vamos colocar uma imagem supimpa???<o:p></o:p></p> <p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='mso-fareast-language:PT-BR'> <img width=248 height=628 id=""Imagem_x0020_1"" src=""cid:[email protected]""></span><o:p></o:p></p> </div> </body> </html>"; var converter = new HtmlToText(html); Assert.Equal("Vamos colocar uma imagem supimpa???", converter.GetText()); }
override protected void GetNextRecord() { ProductVersionsViewItem productVersion = ProductVersionsView.GetProductVersionsViewItem(_loginUser, _itemIDList[_rowIndex]); _lastItemID = productVersion.ProductVersionID; UpdatedItems.Add((int)_lastItemID); DocText = HtmlToText.ConvertHtml(productVersion.Description == null ? string.Empty : productVersion.Description); _docFields.Clear(); foreach (DataColumn column in productVersion.Collection.Table.Columns) { object value = productVersion.Row[column]; string s = value == null || value == DBNull.Value ? "" : value.ToString(); AddDocField(column.ColumnName, s); } ProductVersionsSearch productVersionsSearch = new ProductVersionsSearch(productVersion); Tickets tickets = new Tickets(_loginUser); productVersionsSearch.openTicketCount = tickets.GetProductVersionTicketCount(productVersion.ProductVersionID, 0, _organizationID); AddDocField("**JSON", JsonConvert.SerializeObject(productVersionsSearch)); CustomValues customValues = new CustomValues(_loginUser); customValues.LoadByReferenceType(_organizationID, ReferenceType.ProductVersions, null, productVersion.ProductVersionID); foreach (CustomValue value in customValues) { object o = value.Row["CustomValue"]; string s = o == null || o == DBNull.Value ? "" : o.ToString(); AddDocField(value.Row["Name"].ToString(), s); } DocFields = _docFields.ToString(); DocIsFile = false; DocName = productVersion.ProductVersionID.ToString(); DocCreatedDate = productVersion.DateCreatedUtc; DocModifiedDate = DateTime.UtcNow; }
public ActionResult DisplayReplySection(int page, int randRid) { var UserId = User.Identity.GetUserId(); var loggedUserByUserId = _applicationDbContext.Users.SingleOrDefault(i => i.Id == UserId); if (loggedUserByUserId.Career == "General") { CommentReplyViewModel replyModel = new CommentReplyViewModel(); HtmlToText convert = new HtmlToText(); var threadById = _applicationDbContext.Threads.Where(m => m.ThreadId == page).SingleOrDefault(); var ReportByID = _applicationDbContext.Reports.Where(m => m.ReportId == randRid).SingleOrDefault(); string rawPageTitle = convert.Convert(ReportByID.ReportText).Substring(0, 90); int indexP = rawPageTitle.LastIndexOf(' '); string titleOutput = rawPageTitle.Substring(0, indexP); string PageTitle = ReportByID.CompanyorIndividual + " : " + titleOutput; string sm_PageTitle = Regex.Replace(PageTitle, "[^A-Za-z0-9]", "-"); //string myString = threadById.ThreadText; replyModel.ThreadId = threadById.ThreadId; replyModel.ThreadText = convert.Convert(threadById.ThreadText); replyModel.PageTitle = sm_PageTitle; replyModel.ReportId = ReportByID.ReportId; replyModel.RandomId = Guid.NewGuid().ToString(); return(PartialView("_ReplyModal", replyModel)); } else { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); return(RedirectToAction("Login", "Account")); } }
override protected void GetNextRecord() { WaterCoolerViewItem waterCooler = WaterCoolerView.GetWaterCoolerViewItem(_loginUser, _itemIDList[_rowIndex]); _lastItemID = waterCooler.MessageID; UpdatedItems.Add((int)_lastItemID); DocText = HtmlToText.ConvertHtml(waterCooler.Message); _docFields.Clear(); foreach (DataColumn column in waterCooler.Collection.Table.Columns) { object value = waterCooler.Row[column]; string s = value == null || value == DBNull.Value ? "" : value.ToString(); AddDocField(column.ColumnName, s); } DocFields = _docFields.ToString(); DocIsFile = false; DocName = waterCooler.MessageID.ToString(); DocCreatedDate = waterCooler.TimeStampUtc; DocModifiedDate = DateTime.UtcNow; }
override protected void GetNextRecord() { NotesViewItem note = NotesView.GetNotesViewItem(_loginUser, _itemIDList[_rowIndex]); _lastItemID = note.NoteID; UpdatedItems.Add((int)_lastItemID); DocText = HtmlToText.ConvertHtml(note.Description); _docFields.Clear(); foreach (DataColumn column in note.Collection.Table.Columns) { object value = note.Row[column]; string s = value == null || value == DBNull.Value ? "" : value.ToString(); AddDocField(column.ColumnName, s); } DocFields = _docFields.ToString(); DocIsFile = false; DocName = note.NoteID.ToString(); DocCreatedDate = note.DateCreatedUtc; DocModifiedDate = DateTime.UtcNow; }
private void UpdateText() { if (Control == null || Element == null) { return; } if (string.IsNullOrEmpty(Control.Text)) { return; } var isHtml = HtmlLabel.GetIsHtml(Element); if (!isHtml) { return; } var removeTags = HtmlLabel.GetRemoveHtmlTags(Element); var text = removeTags ? HtmlToText.ConvertHtml(Control.Text) : Element.Text; var helper = new LabelRendererHelper(Element, text); try { CreateAttributedString(Control, helper.ToString()); SetNeedsDisplay(); } catch { // ignored } }
public string MakeLink(string txt) { //Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase); //(http://|)(www\.)?([^\.]+)\.(\w{2})$ //(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*) string testb = HtmlToText.ConvertHtml(txt); string fixedurl; //var resultString = new StringBuilder(testb); Regex regx = new Regex(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)", RegexOptions.IgnoreCase); string resultString = regx.Replace(txt, (match) => { fixedurl = (match.Value.StartsWith("http://") || match.Value.StartsWith("https://")) ? match.Value : "http://" + match.Value; return("<a target='_blank' class='ts-link ui-state-default' href='" + fixedurl + "'>" + match.Value + "</a>"); }); // Regex regx = new Regex(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)", RegexOptions.IgnoreCase); //MatchCollection mactches = regx.Matches(txt); //foreach (Match match in mactches) //{ // if(match.Value.StartsWith("http://") || match.Value.StartsWith("https://")) // fixedurl = match.Value; // else // fixedurl = "http://" + match.Value; // resultString.Replace(match.Value, "<a target='_blank' class='ts-link ui-state-default' href='" + fixedurl + "'>" + match.Value + "</a>"); // //testb = testb.Replace(match.Value, "<a target='_blank' class='ts-link ui-state-default' href='" + fixedurl + "'>" + match.Value + "</a>"); //} return(GenerateTicketLink(resultString.ToString())); }
public static string HtmlToText(string html, bool shouldUseNarrowGapForPTagHtmlToTextConversion) { if (string.IsNullOrEmpty(html)) { return(html); } html = TextConverterHelper.RemoveHtmlLink(html); string result; using (StringReader stringReader = new StringReader(html)) { using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture)) { HtmlToText htmlToText = new HtmlToText(); htmlToText.InputEncoding = Encoding.UTF8; htmlToText.OutputEncoding = Encoding.UTF8; htmlToText.ShouldUseNarrowGapForPTagHtmlToTextConversion = shouldUseNarrowGapForPTagHtmlToTextConversion; TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToText, new ImageRenderingCallback(TextConverterHelper.RemoveImageCallback)); htmlToText.Convert(stringReader, stringWriter); result = stringWriter.ToString(); } } return(result); }
private void SaveAsTextButton_Click(object sender, EventArgs e) { // Create an instance of the save file dialog box. var saveFileDialog1 = new SaveFileDialog { // ReSharper disable once LocalizableElement Filter = "TXT Files (.txt)|*.txt", FilterIndex = 1 }; if (Directory.Exists(Settings.Default.SaveDirectory)) { saveFileDialog1.InitialDirectory = Settings.Default.SaveDirectory; } // Process input if the user clicked OK. if (saveFileDialog1.ShowDialog() == DialogResult.OK) { Settings.Default.SaveDirectory = Path.GetDirectoryName(saveFileDialog1.FileName); var htmlToText = new HtmlToText(); var text = htmlToText.Convert(webBrowser1.DocumentText); File.WriteAllText(saveFileDialog1.FileName, text); } }
public static string format_TextPlain(string htm) { string text = ""; //// Loading HTML Page into DOM Object //IHTMLDocument2 doc = new HTMLDocumentClass(); //doc.clear(); //doc.write(htm); //doc.close(); ////' The following is a must do, to make sure that the data is fully load. //while (doc.readyState != "complete") //{ // //This is also a important part, without this DoEvents() appz hangs on to the “loading” // //System.Windows.Forms.Application.DoEvents(); // ; //} //string source = doc.body.outerHTML; //text = doc.body.innerText.Trim(); text = new HtmlToText().ConvertToTextPlain(htm); return(text); }
public void MustProcessHtmlWithUnicodeChars() { // TODO: A string criada abaixo não está representando um texto unicode real. Verificar para validar. var html = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(@" <html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:w=""urn:schemas-microsoft-com:office:word"" xmlns:m=""http://schemas.microsoft.com/office/2004/12/omml"" xmlns=""http://www.w3.org/TR/REC-html40""> <head> <style> * {behavior:url(#default#VML);} </style> </head> <body lang=PT-BR link=""#0563C1"" vlink=""#954F72""> <p class=3DMsoNormal>Esta mensagem foi verificada pelo sistema de antivírus e acredita-se estar livre de perigo.</p> </body> <script> function test(){ alert('This is a Test'); } </script> </html>")); var converter = new HtmlToText(html); var result = converter.GetText(); Assert.Equal("Esta mensagem foi verificada pelo sistema de antivírus e acredita-se estar livre de perigo.", result); }
public async Task <ActionResult> Create([Bind(Include = "CommentId,ThreadId,UserId,CommentText,DateCreated")] CommentReplyViewModel comment) { HtmlToText convert = new HtmlToText(); Comment com = new Comment(); com.ThreadId = comment.ThreadId; com.CommentText = comment.CommentText; com.UserId = User.Identity.GetUserId(); com.DateCreated = DateTime.UtcNow; if (ModelState.IsValid) { _applicationDbContext.Comments.Add(com); await _applicationDbContext.SaveChangesAsync(); return(Json(new { success = true })); } else { } return(View()); }
public async Task <ActionResult> CreateThread([Bind(Include = "ThreadId,ReportId,UserId,ThreadText,DateCreated")] CommentForReportViewModel comment) { System.Threading.Thread.Sleep(1000); HtmlToText convert = new HtmlToText(); ManageMessageId?message; Thread thread = new Thread(); var reportById = _applicationDbContext.Reports.Where(m => m.ReportId == comment.ReportId).SingleOrDefault(); string RandomId = Guid.NewGuid().ToString(); string PageTitle = reportById.CompanyorIndividual + " : " + convert.Convert(reportById.ReportText).Substring(0, 50); string sm_PageTitle = Regex.Replace(PageTitle, "[^A-Za-z0-9]", "-"); thread.ReportId = comment.ReportId; thread.ThreadText = comment.ThreadText; thread.UserId = User.Identity.GetUserId(); thread.DateCreated = DateTime.UtcNow; if (comment.ThreadText != null) { if (ModelState.IsValid) { _applicationDbContext.Threads.Add(thread); await _applicationDbContext.SaveChangesAsync(); } } else { message = ManageMessageId.Error; return(RedirectToAction("Thread", new { Controller = "Report", action = "Thread", title = sm_PageTitle, page = thread.ReportId, id = RandomId, message })); } return(RedirectToAction("ReportDetails", new { Controller = "Report", action = "ReportDetails", title = sm_PageTitle, page = thread.ReportId, id = RandomId })); }
private string DownloadMainStory() { // Get the selected item GUIListItem item = GetSelectedItem(); if (item == null) { return(null); } feed_details feed = (feed_details)item.MusicTag; // Download the story string text = null; try { string data = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(feed.m_link); try { // Use the current user in case an NTLM Proxy or similar is used. // request.Proxy = WebProxy.GetDefaultProxy(); request.Proxy.Credentials = CredentialCache.DefaultCredentials; } catch (Exception) {} HttpWebResponse response = (HttpWebResponse)request.GetResponse(); try { using (Stream stream = response.GetResponseStream()) { Encoding enc; try { enc = Encoding.GetEncoding(response.ContentEncoding); } catch { // Using Default Encoding enc = Encoding.GetEncoding(m_strSiteEncoding); } using (StreamReader r = new StreamReader(stream, enc)) { data = r.ReadToEnd(); } } // Convert html to text HtmlToText html = new HtmlToText(data); text = html.ToString().Trim(); } finally { if (response != null) { response.Close(); } } } catch (Exception ex) { GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SHOW_WARNING, 0, 0, 0, 0, 0, 0); msg.Param1 = 9; //my news msg.Param2 = 912; //Unable to download latest news msg.Param3 = 0; string errtxt = ex.Message; int pos = errtxt.IndexOf(":"); if (pos != -1) { errtxt = errtxt.Substring(pos + 1); } msg.Label3 = String.Format("{0}\n\n({1})", m_strSiteURL, errtxt); GUIWindowManager.SendMessage(msg); // Log exception Log.Info("ex:{0} {1} {2}", ex.Message, ex.Source, ex.StackTrace); } return(text); }
public ActionResult Index(int?page) { UserReportViewModel viewModel = new UserReportViewModel(); IQueryable <Report> reports = _applicationDbContext.Reports .Include(t => t.Topic).Include(c => c.Category).Include(r => r.ReportImages) .Include(reb => reb.Rebuttals).Include(th => th.Threads).Include(rcu => rcu.RipoffCaseUpdates) .Include(upd => upd.ReportUpdates).Include(v => v.ReportVideos) .OrderByDescending(d => d.DateCreated); var reportList = new List <ReportsWithOwner>(); var questList = new List <TopTenQuestionForLatestReportViewModel>(); foreach (var m in reports) { var models = new ReportsWithOwner(); HtmlToText convert = new HtmlToText(); models.RandomId = Guid.NewGuid().ToString(); string PageTitle = m.CompanyorIndividual + " : " + convert.Convert(m.ReportText).Substring(0, 50); string sm_PageTitle = Regex.Replace(PageTitle, "[^A-Za-z0-9]", "-"); models.PageTitle = sm_PageTitle; models.ReportId = m.ReportId; models.CompanyorIndividual = m.CompanyorIndividual; //string reportText = convert.Convert(m.ReportText); models.ReportText = convert.Convert(m.ReportText); models.DateCreated = m.DateCreated; models.CategoryName = m.Category.Name; models.TopicName = m.Topic.Name; var User = UserManager.FindByIdAsync(m.UserId); var ReportOwner = User.Result.NameExtension; //var CatName = _applicationDbContext.Categories models.DisplayName = ReportOwner; if (m.ReportImages.Count > 0) { models.ReportImagesExist = true; models.ReportImagesCount = m.ReportImages.Count; } if (m.Rebuttals.Count > 0) { models.RebuttalsExist = true; models.RebuttalsCount = m.Rebuttals.Count; } if (m.Threads.Count > 0) { models.ThreadsExist = true; models.ThreadsCount = m.Threads.Count; } //if () //{ // models.CaseUpdatesExist = true; //} if (m.ReportUpdates.Count > 0) { models.ReportUpdateExist = true; models.ReportUpdateCount = m.ReportUpdates.Count; } if (m.ReportVideos.Count > 0) { models.ReportVideosExist = true; models.ReportVideoCount = m.ReportVideos.Count; } else { } reportList.Add(models); } //var topTenQuestion = _applicationDbContext.Questions // .OrderByDescending(dt => dt.DateAsked) // .Take(10); //foreach(var top in topTenQuestion) //{ // TopTenQuestionForLatestReportViewModel questModel = new TopTenQuestionForLatestReportViewModel(); // questModel.QuestionId = top.QuestionId; // questModel.QuestionTitle = top.Title; // questModel.QuestionText = top.QuestionText; // var QuestOwner = UserManager.FindByIdAsync(top.UserId); // string QuestionOwner = QuestOwner.Result.NameExtension; // questModel.QuestionOwner = QuestionOwner; // questModel.DateAsked = top.DateAsked.ToString("d,MMMM yy"); // questModel.TimeAsked = top.DateAsked.ToString("H:mm tt"); // questList.Add(questModel); //} int pageSize = 25; int pageNumber = page ?? 1; //viewModel.TopTenQuestionForLatestReportViewModels = questList; viewModel.ReportsWithOwners = reportList.ToPagedList <ReportsWithOwner>(pageNumber, pageSize); return(View(viewModel)); }