/* * SonarQube * Copyright (C) 2009-2021 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.RegexChannel; /** * Headings are triggered by equal signs at the beginning of a line. The depth of the heading is determined by the number * of equal signs (up to 6). * * E.g., the input: *
 * = Level 1
 * == Level 2
 * === Level 3
 * ==== Level 4
 * ===== Level 5
 * ====== Level 6
 * 
* will produce: *
 * {@literal

}Level 1{@literal

} * {@literal

}Level 2{@literal

} * {@literal

}Level 3{@literal

} * {@literal

}Level 4{@literal

} * {@literal
}Level 5{@literal
} * {@literal
}Level 6{@literal
} *
* @since 4.4 * */ public class HtmlHeadingChannel extends RegexChannel { private static final int MAX_HEADING_DEPTH = 6; public HtmlHeadingChannel() { super("\\s*=+\\s[^\r\n]*+[\r\n]*"); } @Override protected void consume(CharSequence token, MarkdownOutput output) { int index = 0; int headingLevel = 0; while(index < token.length() && Character.isWhitespace(token.charAt(index))) { index ++; } while(index < token.length() && index <= MAX_HEADING_DEPTH && token.charAt(index) == '=') { index ++; headingLevel ++; } while(index < token.length() && Character.isWhitespace(token.charAt(index))) { index ++; } CharSequence headingText = token.subSequence(index, token.length()); output.append(""); output.append(headingText.toString().trim()); output.append(""); } }