public static string ReplaceHtmlElement(this string html, Func<Xml.IXmlNode, Sgml.ISgmlWriter, bool> replacer) { if (string.IsNullOrEmpty(html)) return html; html = html.Trim(); if (!html.StartsWith("<!DOCTYPE") && !(html.StartsWith("<html", StringComparison.InvariantCultureIgnoreCase) && html.EndsWith("</html>", StringComparison.InvariantCultureIgnoreCase))) { html = "<html__>" + html + "</html__>"; } var reader = new Pipes.IO.StringTextSource(html).Pipe(new Pipes.Sgml.SgmlTextReader()); var stackCount = 0; using (var writer = new System.IO.StringWriter()) { using (var xml = Pipes.Sgml.HtmlTextWriter.Create(writer)) { foreach (var item in reader) { if (item.Name.LocalName == "html__") { // Do nothing } else if (stackCount > 0) { // Make sure to skip the entire element including all children switch (item.Type) { case XmlNodeType.Element: stackCount += 1; break; case XmlNodeType.EndElement: stackCount -= 1; break; } } else if (replacer != null && replacer.Invoke(item, xml)) { stackCount = (item.Type == XmlNodeType.Element ? 1 : 0); } else { xml.Node(item); } } xml.Flush(); } return writer.ToString(); } }
public string Convert(string html) { var reader = new Pipes.IO.StringTextSource(html).Pipe(new Pipes.Sgml.SgmlTextReader()); var result = new System.Text.StringBuilder(); foreach (var node in reader) { switch (node.Type) { case Xml.XmlNodeType.Element: break; case Xml.XmlNodeType.EndElement: if(node.Name.LocalName.ToLowerInvariant() == "p") result.AppendLine(); break; case Xml.XmlNodeType.Text: result.Append(node.Value); break; } } return result.ToString().Trim(); }
public void Paste() { if (this.IsCurrentCellInEditMode) { // do nothing } else if (!string.IsNullOrEmpty(Clipboard.GetText())) { var reader = new Pipes.IO.StringTextSource(Clipboard.GetText().TrimEnd('\r', '\n')) .Pipe(new Pipes.Data.DelimitedTextLineReader()); reader.AddDelim('\t'); reader.FieldEnclosingChar = '"'; reader.EnclosingCharEscape = '"'; var lines = reader.ToArray(); var clipRows = lines.Length; string[] fields = null; DataGridViewCell cell; var visColumns = Enumerable.Range(0, this.ColumnCount) .Where(c => this.Columns[c].Visible) .OrderBy(c => this.Columns[c].DisplayIndex).ToArray(); var selectMatrix = (from r in Enumerable.Range(0, this.RowCount) select (from c in visColumns select this[c, r].Selected).ToList()).ToList(); var pasteErrorCount = 0; var pasteErrors = new StringBuilder(); for (var row = 0; row <= this.RowCount - 1; row++) { for (var colIdx = 0; colIdx < visColumns.Length; colIdx++) { if (selectMatrix[row][colIdx]) { var maxRows = this.AllowUserToAddRows ? clipRows : Math.Min(clipRows, this.RowCount - row); for (var pasteRow = 0; pasteRow < maxRows; pasteRow++) { fields = lines[pasteRow].ToArray(); if (this.Rows[row + pasteRow].IsNewRow) { for (var pRow = pasteRow; pRow < maxRows; pRow++) { fields = lines[pRow].ToArray(); var newRow = CreateRow(); for (var pasteCol = 0; pasteCol < Math.Min(fields.Length, visColumns.Length - colIdx); pasteCol++) { cell = this[visColumns[colIdx + pasteCol], row + pasteRow]; if (!cell.ReadOnly) { try { newRow[this.Columns[visColumns[colIdx + pasteCol]].DataPropertyName] = cell.ParseFormattedValue(fields[pasteCol], cell.Style, null, null); } catch (FormatException) { if (pasteErrorCount < 3) { pasteErrors.AppendFormat(" '{0}' could not be pasted in {1} of type {2}" , fields[pasteCol] , cell.OwningColumn.HeaderText ?? cell.OwningColumn.Name , cell.OwningColumn.ValueType.Name).AppendLine(); } pasteErrorCount++; } } } newRow.Add(); } if (pasteRow == 0) this.Rows.RemoveAt(this.RowCount - 2); return; } else { for (var pasteCol = 0; pasteCol < Math.Min(fields.Length, visColumns.Length - colIdx); pasteCol++) { if ((row + pasteRow) < selectMatrix.Count) selectMatrix[row + pasteRow][colIdx + pasteCol] = false; // Prevent overwriting paste area cell = this[visColumns[colIdx + pasteCol], row + pasteRow]; if (!cell.ReadOnly) { try { cell.Value = cell.ParseFormattedValue(fields[pasteCol], cell.Style, null, null); } catch (FormatException) { if (pasteErrorCount < 3) { pasteErrors.AppendFormat(" '{0}' could not be pasted in {1} of type {2}" , fields[pasteCol] , cell.OwningColumn.HeaderText ?? cell.OwningColumn.Name , cell.OwningColumn.ValueType.Name).AppendLine(); } pasteErrorCount++; } } } } } } } } if (pasteErrorCount > 0) { var pasteError = new Exception(string.Format("Could not paste {0} values. For example:\r\n{1}", pasteErrorCount, pasteErrors.ToString())); Utils.HandleError(pasteError); } } }