All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.markdown.HtmlBlockquoteChannel Maven / Gradle / Ivy

/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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:
 * 
 * > Yesterday it worked
 * > Today it is not working
 * > Software is like that
 * 
* will produce: * {@literal
}{@literal

}Yesterday it worked{@literal
} * Today it is not working{@literal
} * Software is like that{@literal

} * @since 4.4 */ class HtmlBlockquoteChannel extends Channel { 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 && quotedLineElement.consume(code, output)) { while (endOfLine.consume(code, output) && quotedLineElement.consume(code, output)) { // consume input } output.append(""); return true; } return false; } finally { pendingBlockConstruction = false; } } private class QuotedLineElementChannel extends RegexChannel { private QuotedLineElementChannel() { super(">[^\r\n]*+"); } @Override public void consume(CharSequence token, MarkdownOutput output) { if (!pendingBlockConstruction) { output.append("
"); pendingBlockConstruction = true; } output.append(token.subSequence(searchIndexOfFirstCharacter(token), token.length())); output.append("
"); } private int searchIndexOfFirstCharacter(CharSequence token) { int index = 0; while (index < token.length()) { if (token.charAt(index) == '&') { index += 4; while (index < token.length()) { if (token.charAt(index) != ' ') { return index; } index ++; } } index ++; } return token.length(); } } private static final class EndOfLine extends RegexChannel { public EndOfLine() { super("(\r?\n)|(\r)"); } @Override public void consume(CharSequence token, MarkdownOutput output) { output.append(token); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy