Mercurial Hosting > nabble
comparison src/nabble/view/web/template/DocNamespace.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.view.web.template; | |
2 | |
3 import nabble.model.Site; | |
4 import nabble.naml.compiler.Command; | |
5 import nabble.naml.compiler.CommandSpec; | |
6 import nabble.naml.compiler.CompileException; | |
7 import nabble.naml.compiler.IPrintWriter; | |
8 import nabble.naml.compiler.Interpreter; | |
9 import nabble.naml.compiler.JavaCommand; | |
10 import nabble.naml.compiler.Meaning; | |
11 import nabble.naml.compiler.Namespace; | |
12 import nabble.naml.compiler.ScopedInterpreter; | |
13 import nabble.naml.namespaces.ListSequence; | |
14 import nabble.naml.namespaces.StringList; | |
15 | |
16 import javax.servlet.ServletException; | |
17 import java.io.IOException; | |
18 import java.lang.reflect.Method; | |
19 import java.util.ArrayList; | |
20 import java.util.Arrays; | |
21 import java.util.Collection; | |
22 import java.util.Collections; | |
23 import java.util.HashMap; | |
24 import java.util.Iterator; | |
25 import java.util.List; | |
26 import java.util.Map; | |
27 | |
28 | |
29 @Namespace ( | |
30 name = "doc", | |
31 global = true | |
32 ) | |
33 public class DocNamespace { | |
34 | |
35 static String getDocName(String macroName,Collection<String> requiredNamespaces) { | |
36 StringBuilder buf = new StringBuilder(); | |
37 buf.append( "doc for " ).append( macroName ); | |
38 if( !requiredNamespaces.isEmpty() ) { | |
39 Iterator<String> iter = requiredNamespaces.iterator(); | |
40 buf.append( " requiring " ).append( iter.next() ); | |
41 while( iter.hasNext() ) { | |
42 buf.append( "," ).append( iter.next() ); | |
43 } | |
44 } | |
45 return buf.toString(); | |
46 } | |
47 | |
48 | |
49 @Namespace ( | |
50 name = "binary_doc", | |
51 global = true | |
52 ) | |
53 public static final class BinaryDocNamespace extends DocNamespace { | |
54 | |
55 private final Site site; | |
56 private final JavaCommand meaning; | |
57 | |
58 private final String value; | |
59 private final String[] params; | |
60 private final String[] seeAlso; | |
61 | |
62 BinaryDocNamespace(Site site, JavaCommand meaning,String value, String[] params, String[] seeAlso) { | |
63 this.site = site; | |
64 this.meaning = meaning; | |
65 this.value = value; | |
66 this.params = params; | |
67 this.seeAlso = seeAlso; | |
68 } | |
69 | |
70 @Command("value") public void _value(IPrintWriter out,Interpreter interp) { | |
71 out.print(value); | |
72 } | |
73 | |
74 @Command public void has_parameters(IPrintWriter out,Interpreter interp) { | |
75 out.print(params.length > 0 || meaning.getParameterNames().length > 0); | |
76 } | |
77 | |
78 public static final CommandSpec parameter_list = CommandSpec.DO; | |
79 | |
80 @Command public void parameter_list(IPrintWriter out, ScopedInterpreter<ParameterList> interp) { | |
81 String[] parameters = meaning.getParameterNames(); | |
82 String[] requiredParameters = meaning.getRequiredParameterNames(); | |
83 String dotParameter = meaning.getDotParameterName(); | |
84 | |
85 HashMap<String,String> all = new HashMap<String,String>(); | |
86 for (String s : params) { | |
87 String[] keyValue = s.split("="); | |
88 String name = keyValue[0]; | |
89 String description = keyValue[1]; | |
90 all.put(name, description); | |
91 } | |
92 for (String s : parameters) { | |
93 if (!all.containsKey(s)) | |
94 all.put(s, ""); | |
95 } | |
96 List<ParameterInfo> infos = new ArrayList<ParameterInfo>(); | |
97 for( Map.Entry<String,String> entry : all.entrySet() ) { | |
98 String name = entry.getKey(); | |
99 String description = entry.getValue(); | |
100 boolean isOptional = !contains(name, requiredParameters); | |
101 boolean isDotParameter = name.equals(dotParameter); | |
102 infos.add(new ParameterInfo(name, description, isOptional, isDotParameter)); | |
103 } | |
104 out.print(interp.getArg(new ParameterList(infos),"do")); | |
105 } | |
106 | |
107 private boolean contains(String name, String[] names) { | |
108 for (String s : names) | |
109 if (s.equals(name)) | |
110 return true; | |
111 return false; | |
112 } | |
113 | |
114 public static final CommandSpec see_also = CommandSpec.DO; | |
115 | |
116 @Command public void see_also(IPrintWriter out, ScopedInterpreter<StringList> interp) { | |
117 out.print(interp.getArg(new StringList(Arrays.asList(seeAlso)),"do")); | |
118 } | |
119 | |
120 @Command public void requires(IPrintWriter out, Interpreter interp) { | |
121 out.print( meaning.getRequiredNamespaces().iterator().next() ); | |
122 } | |
123 | |
124 @Command public void adds_namespace(IPrintWriter out, Interpreter interp) { | |
125 out.print( meaning.addsNamespace() != null ); | |
126 } | |
127 | |
128 @Command public void added_namespace(IPrintWriter out, Interpreter interp) { | |
129 out.print( meaning.addsNamespace() ); | |
130 } | |
131 | |
132 @Command public void binary_details(IPrintWriter out,Interpreter interp) { | |
133 out.print(meaning.getMethod()); | |
134 } | |
135 | |
136 public static final CommandSpec related_commands = CommandSpec.DO; | |
137 | |
138 @Command public void related_commands(IPrintWriter out,ScopedInterpreter<MacroSourceNamespace.Commands> interp) | |
139 throws IOException, ServletException, CompileException | |
140 { | |
141 List<MacroSourceNamespace.CommandInfo> commands = relatedCommands(); | |
142 Collections.sort(commands, MacroSourceNamespace.CommandInfo.MACRO_NAME_COMPARATOR); | |
143 Object block = interp.getArg(new MacroSourceNamespace.Commands(commands),"do"); | |
144 out.print(block); | |
145 } | |
146 | |
147 private List<MacroSourceNamespace.CommandInfo> relatedCommands() { | |
148 List<MacroSourceNamespace.CommandInfo> commands = new ArrayList<MacroSourceNamespace.CommandInfo>(); | |
149 Class c = meaning.getMethod().getDeclaringClass(); | |
150 for (Method method : c.getMethods()) { | |
151 if (method.getAnnotation(Command.class) != null) { | |
152 Meaning m = site.getProgram().getMeaning(c.getName() + '.' + method.getName()); | |
153 if (m != null) | |
154 commands.add(new MacroSourceNamespace.CommandInfo(m, null, null)); | |
155 } | |
156 } | |
157 return commands; | |
158 } | |
159 } | |
160 | |
161 @Namespace ( | |
162 name = "parameter_list", | |
163 global = true | |
164 ) | |
165 public static final class ParameterList extends ListSequence<ParameterInfo> { | |
166 | |
167 public ParameterList(List<ParameterInfo> elements) { | |
168 super(elements); | |
169 } | |
170 | |
171 public static final CommandSpec current_parameter = CommandSpec.DO; | |
172 | |
173 @Command public void current_parameter(IPrintWriter out,ScopedInterpreter<ParameterInfo> interp) { | |
174 out.print(interp.getArg(get(),"do")); | |
175 } | |
176 } | |
177 | |
178 @Namespace ( | |
179 name = "parameter_info", | |
180 global = true | |
181 ) | |
182 public static final class ParameterInfo { | |
183 | |
184 private final String name; | |
185 private final String description; | |
186 private final boolean isOptional; | |
187 private final boolean isDotParameter; | |
188 | |
189 public ParameterInfo(String name, String description, boolean isOptional, boolean isDotParameter) { | |
190 this.name = name; | |
191 this.description = description; | |
192 this.isOptional = isOptional; | |
193 this.isDotParameter = isDotParameter; | |
194 } | |
195 | |
196 @Command("name") public void _name(IPrintWriter out,Interpreter interp) { | |
197 out.print(name); | |
198 } | |
199 | |
200 @Command("description") public void _description(IPrintWriter out,Interpreter interp) { | |
201 out.print(description); | |
202 } | |
203 | |
204 @Command public void is_optional(IPrintWriter out,Interpreter interp) { | |
205 out.print(isOptional); | |
206 } | |
207 | |
208 @Command public void is_dot_parameter(IPrintWriter out,Interpreter interp) { | |
209 out.print(isDotParameter); | |
210 } | |
211 } | |
212 | |
213 } |