]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Reference.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / markdown / nodes / Reference.java
1 package org.simantics.scl.compiler.markdown.nodes;
2
3 import org.simantics.scl.compiler.markdown.inlines.Entities;
4 import org.simantics.scl.compiler.markdown.inlines.Subject;
5
6 public class Reference {
7     public final String label;
8     public final String url;
9     public final String title;
10     
11     public Reference(String label, String url, String title) {
12         this.label = label;
13         this.url = url;
14         this.title = title;
15     }
16     
17     public static String normalizeLabel(String label) {
18         StringBuilder b = new StringBuilder();
19         int pos = 0;
20         while(pos < label.length() && Character.isWhitespace(label.charAt(pos)))
21             ++pos;
22         boolean ws = false;
23         while(pos < label.length()) {
24             char c = label.charAt(pos++);
25             if(Character.isWhitespace(c))
26                 ws = true;
27             else {
28                 if(ws)
29                     b.append(' ');
30                 b.append(Character.toLowerCase(c));
31             }
32         }
33         return b.toString();
34     }
35     
36     public static String cleanUrl(String input) {
37         StringBuilder b = new StringBuilder();
38         int pos = 0;
39         while(pos < input.length()) {
40             char c = input.charAt(pos++);
41             if(c == '\\' && pos < input.length()) {
42                 c = input.charAt(pos);
43                 if(Subject.getCharType(c) == 2) {
44                     b.append(c);
45                     ++pos;
46                 }
47                 else
48                     b.append('\\');
49             }
50             else if(c == '&') {
51                 int maxPos = Math.min(input.length(), pos+Entities.MAX_ENTITY_LENGTH+1);
52                 int p = pos;
53                 while(true) {
54                     if(p == maxPos) {
55                         b.append("&");
56                         break;
57                     }
58                     c = input.charAt(p++);
59                     if(c == ';') {
60                         String entity = input.substring(pos, p-1);
61                         String character = Entities.ENTITY_MAP.get(entity);
62                         if(character == null) {
63                             b.append("&");
64                             break;
65                         }
66                         else {
67                             pos = p;
68                             b.append(character);
69                             break;
70                         }
71                     }
72                 }
73             }
74             else
75                 b.append(c);
76         }
77         return b.toString();
78     }
79     
80     public static String cleanTitle(String input) {
81         return cleanUrl(input);
82     }
83 }