1 | |
package controllers; |
2 | |
|
3 | |
import play.*; |
4 | |
import play.mvc.*; |
5 | |
import play.libs.*; |
6 | |
import play.vfs.*; |
7 | |
|
8 | |
import java.io.*; |
9 | |
import java.util.*; |
10 | |
|
11 | 0 | public class PlayDocumentation extends Controller { |
12 | |
|
13 | |
public static void index() throws Exception { |
14 | 1 | page("home", null); |
15 | 0 | } |
16 | |
|
17 | |
public static void page(String id, String module) throws Exception { |
18 | 2 | File page = new File(Play.frameworkPath, "documentation/manual/"+id+".textile"); |
19 | 2 | if(module != null) { |
20 | 0 | page = new File(Play.modules.get(module).getRealFile(), "documentation/manual/"+id+".textile"); |
21 | |
} |
22 | 2 | if(!page.exists()) { |
23 | 0 | notFound("Manual page for "+id+" not found"); |
24 | |
} |
25 | 2 | String textile = IO.readContentAsString(page); |
26 | 2 | String html = toHTML(textile); |
27 | 2 | String title = getTitle(textile); |
28 | |
|
29 | 2 | List<String> modules = new ArrayList(); |
30 | 2 | if(id.equals("home") && module == null) { |
31 | 7 | for(String key : Play.modules.keySet()) { |
32 | 5 | VirtualFile mr = Play.modules.get(key); |
33 | 5 | VirtualFile home = mr.child("documentation/manual/home.textile"); |
34 | 5 | if(home.exists()) { |
35 | 1 | modules.add(key); |
36 | |
} |
37 | |
} |
38 | |
} |
39 | |
|
40 | 2 | render(id, html, title, modules, module); |
41 | 0 | } |
42 | |
|
43 | |
public static void image(String name, String module) { |
44 | 0 | File image = new File(Play.frameworkPath, "documentation/images/"+name+".png"); |
45 | 0 | if(module != null) { |
46 | 0 | image = new File(Play.modules.get(module).getRealFile(),"documentation/images/"+name+".png"); |
47 | |
} |
48 | 0 | if(!image.exists()) { |
49 | 0 | notFound(); |
50 | |
} |
51 | 0 | renderBinary(image); |
52 | 0 | } |
53 | |
|
54 | |
public static void file(String name) { |
55 | 0 | File file = new File(Play.frameworkPath, "documentation/files/"+name); |
56 | 0 | if(!file.exists()) { |
57 | 0 | notFound(); |
58 | |
} |
59 | 0 | renderBinary(file); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
static String toHTML(String textile) { |
65 | 2 | String html = new org.eclipse.mylyn.wikitext.core.parser.MarkupParser(new org.eclipse.mylyn.wikitext.textile.core.TextileLanguage()).parseToHtml(textile); |
66 | 2 | html = html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>")); |
67 | 2 | return html; |
68 | |
} |
69 | |
|
70 | |
static String getTitle(String textile) { |
71 | 2 | if(textile.length() == 0) { |
72 | 0 | return ""; |
73 | |
} |
74 | 2 | return textile.split("\n")[0].substring(3).trim(); |
75 | |
} |
76 | |
|
77 | |
} |