1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.markdown;
import org.sonar.channel.Channel;
import org.sonar.channel.CodeReader;
import org.sonar.channel.RegexChannel;
/**
* Markdown treats lines starting with a greater than sign (>) as a block of quoted text.
*
* E.g, the input:
* <pre>
* > Yesterday it worked
* > Today it is not working
* > Software is like that
* </pre>
* will produce:
* {@literal<blockquote>}{@literal<p>}Yesterday it worked{@literal<br/>}
* Today it is not working{@literal<br/>}
* Software is like that{@literal</blockquote>}
* @since 4.4
*/
class HtmlBlockquoteChannel extends Channel<MarkdownOutput> {
private QuotedLineElementChannel quotedLineElement = new QuotedLineElementChannel();
private EndOfLine endOfLine = new EndOfLine();
private boolean pendingBlockConstruction;
@Override
public boolean consume(CodeReader code, MarkdownOutput output) {
try {
if (code.getColumnPosition() == 0) {
if (quotedLineElement.consume(code, output)) {
while (endOfLine.consume(code, output) && quotedLineElement.consume(code, output)) {
// consume input
}
output.append("</blockquote>");
return true;
}
}
return false;
} finally {
pendingBlockConstruction = false;
}
}
private class QuotedLineElementChannel extends RegexChannel<MarkdownOutput> {
protected QuotedLineElementChannel() {
super(">\\s[^\r\n]*+");
}
@Override
protected void consume(CharSequence token, MarkdownOutput output) {
if (!pendingBlockConstruction) {
output.append("<blockquote>");
pendingBlockConstruction = true;
}
output.append(token.subSequence(searchIndexOfFirstCharacter(token), token.length()));
output.append("<br/>");
}
private int searchIndexOfFirstCharacter(CharSequence token) {
for (int index = 0; index < token.length(); index++) {
if (token.charAt(index) == '>') {
while (++ index < token.length()) {
if (token.charAt(index) != ' ') {
return index;
}
}
}
}
return token.length() - 1;
}
}
private static final class EndOfLine extends RegexChannel<MarkdownOutput> {
public EndOfLine() {
super("(\r?\n)|(\r)");
}
@Override
protected void consume(CharSequence token, MarkdownOutput output) {
output.append(token);
}
}
}
|