private void appendTextPreserveIndentation(final int end, final int depth, final int originalIndentLength) throws IOException { assert index<end; appendIndent(depth); appendLineKeepWhiteSpace(end,depth); while (index!=end) { // Skip over the original indent: for (int x=0; x<originalIndentLength; x++) { final char ch=sourceText.charAt(index); if (!(ch==' ' || ch=='\t')) break; if (++index==end) return; } appendEssentialNewLine(); // Insert our indent: appendIndent(depth); // Write the rest of the line including any indent greater than the first line's indent: appendLineKeepWhiteSpace(end,depth); } assert index==end; }
private void appendText(final int end, int depth) throws IOException { assert index<=end; if (index==end) return; while (Segment.isWhiteSpace(sourceText.charAt(index))) if (++index==end) return; // trim whitespace. appendIndent(depth); if (collapseWhiteSpace) { appendTextCollapseWhiteSpace(end,depth); } else { appendTextInline(end,depth,false); } appendFormattingNewLine(); assert index==end; }
private void appendSpecifiedTextInline(final CharSequence text, int depth) throws IOException { final int textLength=text.length(); int i=appendSpecifiedLine(text,0); if (i<textLength) { final int subsequentLineDepth=depth+1; do { while (Segment.isWhiteSpace(text.charAt(i))) if (++i>=textLength) return; // trim whitespace. appendEssentialNewLine(); appendIndent(subsequentLineDepth); i=appendSpecifiedLine(text,i); } while (i<textLength); } }
assert index<tagEnd; if (tag.getTagType()==StartTagType.COMMENT || tag.getTagType()==StartTagType.CDATA_SECTION || tag.getTagType().isServerTag()) { appendTextPreserveIndentation(tagEnd,depth); } else if (tidyTags) { final String tidyTag=tag.tidy(); appendable.append(tidyTag); else appendSpecifiedTextInline(tidyTag,depth); index=tagEnd; } else { appendTextInline(tagEnd,depth,true); // Write tag keeping linefeeds. This will add an indent to any attribute values containing linefeeds, but the normal situation where line breaks are between attributes will look nice. singleLineContent=appendTextInline(contentEnd,depth+1,false); } else { singleLineContent=appendTextPreserveIndentation(contentEnd,depth); appendEssentialNewLine(); // some server or client side scripting languages might need the final new line appendIndent(depth); appendTag(endTag,depth,end);
final StartTag startTag=element.getStartTag(); final EndTag endTag=element.getEndTag(); appendIndent(depth); appendTag(startTag,depth,end); if (index==end) { appendFormattingNewLine(); assert index==Math.min(element.end,end) : index; return; if (!renderContentInline) appendFormattingNewLine(); int contentEnd=element.getContentEnd(); if (end<contentEnd) contentEnd=end; appendContentPreformatted(contentEnd,depth); } else { appendIndentedScriptContent(contentEnd,depth+1); appendTextCollapseWhiteSpace(contentEnd,depth); } else { if (!appendTextInline(contentEnd,depth,true)) { appendFormattingNewLine(); renderContentInline=false; appendContent(contentEnd,element.getChildElements(),depth+1); if (!renderContentInline) appendIndent(depth); assert index==endTag.begin; appendTag(endTag,depth,end);
private void appendIndentedScriptContent(final int end, final int depth) throws IOException { assert index<end; if (removeLineBreaks) { appendTextRemoveIndentation(end); assert index==end; return; } int startOfLinePos=getStartOfLinePos(end,false); if (index==end) return; if (startOfLinePos==-1) { // Script started on same line as start tag. Use the start of the next line to determine the original indent. appendIndent(depth); appendLineKeepWhiteSpace(end,depth); appendEssentialNewLine(); if (index==end) return; startOfLinePos=getStartOfLinePos(end,true); if (index==end) return; } appendTextPreserveIndentation(end,depth,index-startOfLinePos); appendEssentialNewLine(); assert index==end; }
private void appendContent(final int end, final List<Element> childElements, final int depth) throws IOException { assert index<=end; for (Element element : childElements) { final int elementBegin=element.begin; if (elementBegin>=end) break; if (indentAllElements) { appendText(elementBegin,depth); appendElement(element,depth,end,false,false); } else { if (inlinable(element)) continue; // skip over elements that can be inlined. appendText(elementBegin,depth); final String elementName=element.getName(); if (elementName==HTMLElementName.PRE || elementName==HTMLElementName.TEXTAREA) { appendElement(element,depth,end,true,true); } else if (elementName==HTMLElementName.SCRIPT) { appendElement(element,depth,end,true,false); } else { appendElement(element,depth,end,false,!removeLineBreaks && containsOnlyInlineLevelChildElements(element)); } } } appendText(end,depth); assert index==end; }
private boolean appendTextRemoveIndentation(final int end) throws IOException { assert index<end; appendLineKeepWhiteSpace(end,0); if (index==end) return true; while (index!=end) { // Skip over the original indent: while (true) { final char ch=sourceText.charAt(index); if (!(ch==' ' || ch=='\t')) break; if (++index==end) return false; } appendEssentialNewLine(); // Write the rest of the line including any indent greater than the first line's indent: appendLineKeepWhiteSpace(end,0); } assert index==end; return false; }
private boolean appendTextPreserveIndentation(final int end, final int depth) throws IOException { // returns true if all text was on one line, otherwise false assert index<end; if (removeLineBreaks) return appendTextRemoveIndentation(end); // Use the start of the next line to determine the original indent. appendLineKeepWhiteSpace(end,depth); if (index==end) return true; int startOfLinePos=getStartOfLinePos(end,true); if (index==end) return true; appendEssentialNewLine(); appendTextPreserveIndentation(end,depth+1,index-startOfLinePos); assert index==end; return false; }
private boolean appendTextInline(final int end, int depth, final boolean increaseIndentAfterFirstLineBreak) throws IOException { // returns true if all text was on one line, otherwise false assert index<end; appendLineKeepWhiteSpace(end,depth); if (index==end) return true; final int subsequentLineDepth=increaseIndentAfterFirstLineBreak ? depth+1 : depth; do { while (Segment.isWhiteSpace(sourceText.charAt(index))) if (++index==end) return false; // trim whitespace. appendEssentialNewLine(); // essential because we might be inside a tag attribute value. If new lines in normal text aren't required this method wouldn't have been called. appendIndent(subsequentLineDepth); appendLineKeepWhiteSpace(end,subsequentLineDepth); } while (index<end); assert index==end; return false; }
public void appendTo(final Appendable appendable) throws IOException { this.appendable=appendable; if (segment instanceof Source) ((Source)segment).fullSequentialParse(); nextTag=segment.source.getNextTag(segment.begin); index=segment.begin; appendContent(segment.end,segment.getChildElements(),0); }