private string ExportToHTML(Scintilla.ScintillaControl sc) { StringBuilder result = new StringBuilder(); result.Append("<span>"); int tabSize = 4; int styleCurrent = sc.StyleAt(0); result.Append(String.Format("<span class=\"S{0:d}\">", styleCurrent)); bool inStyleSpan = true; int lengthDoc = sc.Length; int column = 0; for (int i = 0; i < lengthDoc; ++i) { char ch = sc.CharAt(i); int style = sc.StyleAt(i); if (style != styleCurrent) { if (inStyleSpan) { result.Append("</span>"); inStyleSpan = false; } if (ch != '\r' && ch != '\n') { // No need of a span for the EOL result.Append(String.Format("<span class=\"S{0:d}\">", style)); inStyleSpan = true; styleCurrent = style; } } if (ch == ' ') { char prevCh = '\0'; if (column == 0) { // At start of line, must put a because regular space will be collapsed prevCh = ' '; } while (i < lengthDoc && sc.CharAt(i) == ' ') { if (prevCh != ' ') { result.Append(' '); } else { result.Append(" "); } prevCh = sc.CharAt(i); i++; column++; } i--; // the last incrementation will be done by the for loop } else if (ch == '\t') { int ts = tabSize - (column % tabSize); for (int itab = 0; itab < ts; itab++) { if (itab % 2 == 1) { result.Append(' '); } else { result.Append(" "); } } column += ts; } else if (ch == '\r' || ch == '\n') { if (inStyleSpan) { result.Append("</span>"); inStyleSpan = false; } if (ch == '\r' && sc.CharAt(i + 1) == '\n') { i++; // CR+LF line ending, skip the "extra" EOL char } column = 0; result.Append("<br />"); styleCurrent = sc.StyleAt(i + 1); result.Append('\n'); if (sc.CharAt(i + 1) != '\r' && sc.CharAt(i + 1) != '\n') { // We know it's the correct next style, // but no (empty) span for an empty line result.Append(String.Format("<span class=\"S{0:d}\">", styleCurrent)); inStyleSpan = true; } } else { switch (ch) { case '<': result.Append("<"); break; case '>': result.Append(">"); break; case '&': result.Append("&"); break; default: result.Append(ch); break; } column++; } } if(inStyleSpan) result.Append("</span>"); result.Append("</span>"); return result.ToString(); }