You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HtmlHeadingChannel.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.markdown;
  21. import org.sonar.channel.RegexChannel;
  22. /**
  23. * Headings are triggered by equal signs at the beginning of a line. The depth of the heading is determined by the number
  24. * of equal signs (up to 6).
  25. *
  26. * E.g., the input:
  27. * <pre>
  28. * = Level 1
  29. * == Level 2
  30. * === Level 3
  31. * ==== Level 4
  32. * ===== Level 5
  33. * ====== Level 6
  34. * </pre>
  35. * will produce:
  36. * <pre>
  37. * {@literal<h1>}Level 1{@literal</h1>}
  38. * {@literal<h2>}Level 2{@literal</h2>}
  39. * {@literal<h3>}Level 3{@literal</h3>}
  40. * {@literal<h4>}Level 4{@literal</h4>}
  41. * {@literal<h5>}Level 5{@literal</h5>}
  42. * {@literal<h6>}Level 6{@literal</h6>}
  43. * </pre>
  44. * @since 4.4
  45. *
  46. */
  47. public class HtmlHeadingChannel extends RegexChannel<MarkdownOutput> {
  48. private static final int MAX_HEADING_DEPTH = 6;
  49. public HtmlHeadingChannel() {
  50. super("\\s*=+\\s[^\r\n]*+[\r\n]*");
  51. }
  52. @Override
  53. protected void consume(CharSequence token, MarkdownOutput output) {
  54. int index = 0;
  55. int headingLevel = 0;
  56. while(index < token.length() && Character.isWhitespace(token.charAt(index))) {
  57. index ++;
  58. }
  59. while(index < token.length() && index <= MAX_HEADING_DEPTH && token.charAt(index) == '=') {
  60. index ++;
  61. headingLevel ++;
  62. }
  63. while(index < token.length() && Character.isWhitespace(token.charAt(index))) {
  64. index ++;
  65. }
  66. CharSequence headingText = token.subSequence(index, token.length());
  67. output.append("<h" + headingLevel + ">");
  68. output.append(headingText.toString().trim());
  69. output.append("</h" + headingLevel + ">");
  70. }
  71. }