parser: regenerate (with qclassid)
[nit.git] / src / web / web_views.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Nitcorn actions used by the nitweb server.
16 module web_views
17
18 import web_base
19 import model_html
20 import highlight
21 import markdown
22
23 # Html homepage for the `nitweb` server.
24 class HtmlHomePage
25 super NitView
26
27 # Loaded model to display.
28 var model: Model
29
30 redef fun render(srv) do
31 var tpl = new Template
32 tpl.add new Header(1, "Loaded model")
33 for mpackage in model.mpackages do
34 tpl.add new Header(3, "Packages")
35 tpl.add mpackage.html_tree
36 end
37 return tpl
38 end
39 end
40
41 # Display a search results list.
42 class HtmlResultPage
43 super NitView
44
45 # Initial query.
46 var query: String
47
48 # Result set
49 var results: Array[MEntity]
50
51 redef fun render(srv) do
52 var tpl = new Template
53 tpl.add new Header(1, "Results for {query}")
54 if results.is_empty then
55 tpl.add "<p>No result for {query}.<p>"
56 return tpl
57 end
58 var list = new UnorderedList
59 for mentity in results do
60 var link = mentity.html_link
61 link.text = mentity.html_raw_namespace
62 list.add_li new ListItem(link)
63 end
64 tpl.add list
65 return tpl
66 end
67 end
68
69 # Display the source for each mentities
70 class HtmlSourcePage
71 super NitView
72
73 # Modelbuilder used to access sources.
74 var modelbuilder: ModelBuilder
75
76 # MEntity to display
77 var mentity: MEntity
78
79 # HiglightVisitor used to hilight the source code
80 var hl = new HighlightVisitor
81
82 redef fun render(srv) do
83 var tpl = new Template
84 tpl.add new Header(1, "Source Code")
85 tpl.add render_source
86 return tpl
87 end
88
89 private fun render_source: Template do
90 var node = modelbuilder.mentity2node(mentity)
91 var tpl = new Template
92 tpl.add new Header(3, "Source code")
93 if node == null then
94 tpl.add "<p>Source for {mentity.html_name} not found.<p>"
95 else
96 hl.enter_visit node
97 tpl.add "<pre><code>"
98 tpl.add hl.html
99 tpl.add "</code></pre>"
100 end
101 return tpl
102 end
103 end
104
105 # Display the mdoc of the mentities.
106 class HtmlDocPage
107 super HtmlSourcePage
108
109 redef fun render(srv) do
110 var tpl = new Template
111 tpl.add new Header(1, mentity.html_name)
112 tpl.add "<p>"
113 tpl.add mentity.html_declaration
114 tpl.add "</p>"
115 tpl.add "<br>"
116 var mdoc = mentity.mdoc
117 if mdoc != null then
118 tpl.add mdoc.content.join("\n").md_to_html
119 end
120 tpl.add "<br>"
121 tpl.add render_source
122 return tpl
123 end
124 end