@Test public void process() { assertContent("<pre><code>code\n</code></pre>", " code"); assertContent("<pre><code>code\n</code></pre>", "\tcode"); assertContent("<pre><code> code\n</code></pre>", "\t code"); assertContent("<pre><code>code \n</code></pre>", "\tcode "); assertContent("<pre><code>\tcode\n</code></pre>", " \tcode"); assertContent("<pre><code>one\ntwo\n</code></pre><p>three</p>", " one\n two\n three"); assertContent("<pre><code>one\n\nthree\n</code></pre>", " one\n\n three"); assertContent("<pre><code>one\n \nthree\n</code></pre>", " one\n \n three"); assertContent("<pre><code>\t\tcode\n</code></pre>", "\t\t\tcode"); }
@Override public void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence) { builder.setLocator(lineSequence.getCurrentLine().toLocator()); builder.beginBlock(BlockType.CODE, new Attributes()); boolean blockHasContent = false; Iterator<Line> iterator = lineSequence .with(Predicates.or(LinePredicates.matches(PATTERN), LinePredicates.empty())).iterator(); while (iterator.hasNext()) { Line line = iterator.next(); Matcher matcher = PATTERN.matcher(line.getText()); if (!matcher.matches()) { checkState(line.isEmpty()); if (iterator.hasNext()) { builder.characters("\n"); } } else { String content = matcher.group(1); if (!content.isEmpty() || (blockHasContent && iterator.hasNext())) { blockHasContent = true; builder.characters(content); builder.characters("\n"); } } } builder.endBlock(); }
IndentedCodeBlock extends SourceBlock { @Override public void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence) { builder.setLocator(lineSequence.getCurrentLine().toLocator()); builder.beginBlock(BlockType.CODE, new Attributes()); boolean blockHasContent = false; Iterator<Line> iterator = lineSequence .with(Predicates.or(LinePredicates.matches(PATTERN), LinePredicates.empty())).iterator(); while (iterator.hasNext()) { Line line = iterator.next(); Matcher matcher = PATTERN.matcher(line.getText()); if (!matcher.matches()) { checkState(line.isEmpty()); if (iterator.hasNext()) { builder.characters("\n"); } } else { String content = matcher.group(1); if (!content.isEmpty() || (blockHasContent && iterator.hasNext())) { blockHasContent = true; builder.characters(content); builder.characters("\n"); } } } builder.endBlock(); } }
IndentedCodeBlock extends SourceBlock { @Override public void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence) { builder.setLocator(lineSequence.getCurrentLine().toLocator()); builder.beginBlock(BlockType.CODE, new Attributes()); boolean blockHasContent = false; Iterator<Line> iterator = lineSequence .with(Predicates.or(LinePredicates.matches(PATTERN), LinePredicates.empty())).iterator(); while (iterator.hasNext()) { Line line = iterator.next(); Matcher matcher = PATTERN.matcher(line.getText()); if (!matcher.matches()) { checkState(line.isEmpty()); if (iterator.hasNext()) { builder.characters("\n"); } } else { String content = matcher.group(1); if (!content.isEmpty() || (blockHasContent && iterator.hasNext())) { blockHasContent = true; builder.characters(content); builder.characters("\n"); } } } builder.endBlock(); }  }
IndentedCodeBlock extends SourceBlock { @Override public void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence) { builder.setLocator(lineSequence.getCurrentLine().toLocator()); builder.beginBlock(BlockType.CODE, new Attributes()); boolean blockHasContent = false; Iterator<Line> iterator = lineSequence .with(Predicates.or(LinePredicates.matches(PATTERN), LinePredicates.empty())).iterator(); while (iterator.hasNext()) { Line line = iterator.next(); Matcher matcher = PATTERN.matcher(line.getText()); if (!matcher.matches()) { checkState(line.isEmpty()); if (iterator.hasNext()) { builder.characters("\n"); } } else { String content = matcher.group(1); if (!content.isEmpty() || (blockHasContent && iterator.hasNext())) { blockHasContent = true; builder.characters(content); builder.characters("\n"); } } } builder.endBlock(); }  @Override void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence); @Override boolean canStart(LineSequence lineSequence); }
IndentedCodeBlock extends SourceBlock { @Override public void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence) { builder.setLocator(lineSequence.getCurrentLine().toLocator()); builder.beginBlock(BlockType.CODE, new Attributes()); boolean blockHasContent = false; Iterator<Line> iterator = lineSequence .with(Predicates.or(LinePredicates.matches(PATTERN), LinePredicates.empty())).iterator(); while (iterator.hasNext()) { Line line = iterator.next(); Matcher matcher = PATTERN.matcher(line.getText()); if (!matcher.matches()) { checkState(line.isEmpty()); if (iterator.hasNext()) { builder.characters("\n"); } } else { String content = matcher.group(1); if (!content.isEmpty() || (blockHasContent && iterator.hasNext())) { blockHasContent = true; builder.characters(content); builder.characters("\n"); } } } builder.endBlock(); }  @Override void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence); @Override boolean canStart(LineSequence lineSequence);  }