aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java
blob: d0140c71dfffac061fe825dae3d6d4f1560b5848 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * SonarQube
 * Copyright (C) 2009-2025 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;

/**
 * Lists come in two flavors:
 * <ul>
 *  <li>Unordered lists, triggered by lines that start with a <code>*</code></li>
 *  <li>Ordered lists (added in 4.4), triggered by lines that start with a digit followed by a <code>.</code></li>
 * </ul>

 * E.g., the input:
 * <pre>
 * * One
 * * Two
 * * Three
 * </pre>
 * will produce:
 * {@literal<ul>}{@literal<li>}One{@literal</li>}
 * {@literal<li>}Two{@literal</li>}
 * {@literal<li>}Three{@literal</li>}{@literal</ul>}
 *
 * Whereas the input:
 * <pre>
 * 1. One
 * 1. Two
 * 1. Three
 * </pre>
 * will produce:
 * {@literal<ol>}{@literal<li>}One{@literal</li>}
 * {@literal<li>}Two{@literal</li>}
 * {@literal<li>}Three{@literal</li>}{@literal</ol>}
 *
 * @since 2.10.1
 */
class HtmlListChannel extends Channel<MarkdownOutput> {

  private OrderedListElementChannel orderedListElement = new OrderedListElementChannel();
  private UnorderedListElementChannel unorderedListElement = new UnorderedListElementChannel();
  private EndOfLine endOfLine = new EndOfLine();
  private boolean pendingListConstruction;

  @Override
  public boolean consume(CodeReader code, MarkdownOutput output) {
    try {
      ListElementChannel currentChannel = null;
      if (code.getColumnPosition() == 0) {
        if (orderedListElement.consume(code, output)) {
          currentChannel = orderedListElement;
        } else if (unorderedListElement.consume(code, output)) {
          currentChannel = unorderedListElement;
        }
        if (currentChannel != null) {
          while (endOfLine.consume(code, output) && currentChannel.consume(code, output)) {
            // consume input
          }
          output.append("</" + currentChannel.listElement + ">");
          return true;
        }
      }
      return false;
    } finally {
      pendingListConstruction = false;
    }
  }

  private class OrderedListElementChannel extends ListElementChannel {
    public OrderedListElementChannel() {
      super("\\d\\.", "ol");
    }
  }

  private class UnorderedListElementChannel extends ListElementChannel {
    public UnorderedListElementChannel() {
      super("\\*", "ul");
    }
  }

  private abstract class ListElementChannel extends RegexChannel<MarkdownOutput> {

    private String listElement;

    protected ListElementChannel(String markerRegexp, String listElement) {
      super("\\s*+" + markerRegexp + "\\s[^\r\n]*+");
      this.listElement = listElement;
    }

    @Override
    protected void consume(CharSequence token, MarkdownOutput output) {
      if (!pendingListConstruction) {
        output.append("<" + listElement + ">");
        pendingListConstruction = true;
      }
      output.append("<li>");
      output.append(token.subSequence(searchIndexOfFirstCharacter(token), token.length()));
      output.append("</li>");
    }

    private int searchIndexOfFirstCharacter(CharSequence token) {
      for (int index = 0; index < token.length(); index++) {
        if (token.charAt(index) == '*'
          || Character.isDigit(token.charAt(index))) {
          if (token.charAt(index + 1) == '.') {
            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);
    }
  }
}