/
HtmlUtil.java
169 lines (149 loc) · 5 KB
/
HtmlUtil.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* (c) 2011, Alejandro Serrano
* Released under the terms of the EPL.
*/
package net.sf.eclipsefp.haskell.browser.util;
import java.util.ArrayList;
import net.sf.eclipsefp.haskell.browser.items.PackageIdentifier;
import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
/**
* Generates Html for showing documentation in several places.
*
* @author Alejandro Serrano
*/
public class HtmlUtil {
private static void initialPart( final StringBuilder builder ) {
builder.append( "<html>" );
builder.append( "<body>" );
builder.append( "<div style=\"font-size: small\">" );
}
private static void finalPart( final StringBuilder builder ) {
builder.append( "</div>" );
builder.append( "</body>" );
builder.append( "</html>" );
}
private static void addDocs( final String docs, final StringBuilder builder ) {
String[] paragraphs = docs.split( "\n\n" );
for( String paragraph: paragraphs ) {
builder.append( "<p>" );
builder.append( paragraph );
builder.append( "</p>" );
}
}
private static void addPackageModule(
final ArrayList<PackageIdentifier> pkgs, final String module,
final StringBuilder builder ) {
builder.append( "<p>" );
if( module != null ) {
builder.append( "<b>" );
builder.append( UITexts.browser_definedInModules );
builder.append( "</b>" );
builder.append( module );
}
if( pkgs != null ) {
if( module != null ) {
builder.append( "<br />" );
}
builder.append( "<b>" );
builder.append( UITexts.browser_packagedInPackages );
builder.append( "</b>" );
boolean first = true;
for( PackageIdentifier pkg: pkgs ) {
if( !first ) {
builder.append( ", " );
}
builder.append( pkg.toString() );
first = false;
}
}
builder.append( "</p>" );
}
/**
* Complete way to generate documentation.
*
* @param definition
* complete definition of the item to show
* @param pkgs
* list of packages where it is defined, or null to not show it
* @param module
* module where it is defined, or null to not show it
* @param inAtStart
* if true, the information about the item package+module will be
* shown before the docs, if false, it will be after
* @param docs
* documentation for the item
*/
public static String generateDocument( final String definition,
final ArrayList<PackageIdentifier> pkgs, final String module,
final boolean inAtStart, final String docs ) {
StringBuilder builder = new StringBuilder();
initialPart( builder );
if( definition != null ) {
builder.append( "<p style=\"font-family: monospace\">" );
builder.append( definition );
builder.append( "</p>" );
}
if( inAtStart ) {
addPackageModule( pkgs, module, builder );
}
addDocs( docs, builder );
if( !inAtStart ) {
addPackageModule( pkgs, module, builder );
}
finalPart( builder );
return builder.toString();
}
/**
* Complete way to generate documentation, equivalent to
* {@link HtmlUtil#generateDocument(String, ArrayList, String, boolean, String)}
* but without package and module info.
*/
public static String generateDocument( final String definition,
final String docs ) {
return generateDocument( definition, null, null, true, docs );
}
public static String generateText ( final String text ) {
StringBuilder builder = new StringBuilder();
initialPart( builder );
builder.append( text );
finalPart( builder );
return builder.toString();
}
public static String generatePackageUrl( final PackageIdentifier item ) {
if( item.getName().equals( "ghc" ) ) {
// GHC libraries are a special case
return "http://www.haskell.org/ghc/docs/" + item.getVersion()
+ "/html/libraries/" + item.toString() + "/";
} else {
return "http://hackage.haskell.org/package/" + item.toString();
}
}
public static String generateModuleUrl( final PackageIdentifier pkg, final String module ) {
String url;
if( pkg.getName().equals( "ghc" ) ) {
// GHC libraries are a special case
url = "http://www.haskell.org/ghc/docs/" + pkg.getVersion()
+ "/html/libraries/" + pkg.toString() + "/";
} else {
url = "http://hackage.haskell.org/packages/archive/" + pkg.getName()
+ "/" + pkg.getVersion() + "/doc/html/";
}
// Add module name
url += module.replace( '.', '-' ) + ".html";
return url;
}
public static String generateElementUrl( final PackageIdentifier pkg, final String module, final boolean isFunctionLike, final String name ) {
String url = generateModuleUrl( pkg, module );
// Add declaration name
String itemName = name;
if( itemName.startsWith( "(" ) ) {
itemName = itemName.substring( 1, itemName.length() - 1 );
}
if( isFunctionLike ) {
url += "#v:" + itemName;
} else {
url += "#t:" + itemName;
}
return url;
}
}