AI2 Component  (Version nb184)
MarkdownDocumentationGenerator.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright © 2019 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.scripts;
7 
8 import java.io.BufferedWriter;
9 import java.io.IOException;
10 import java.io.Writer;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.TreeMap;
15 
17 
18  private static final String COLOR_TYPE = "color";
19 
20  @Override
21  protected void outputResults() throws IOException {
22 
23  Map<String, String> categoryNames = new HashMap<>();
24  Map<String, StringBuilder> categoryDocs = new HashMap<>();
25  Map<String, Map<String, ComponentInfo>> sortedComponents = new HashMap<>();
26 
27  for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
28  ComponentInfo component = entry.getValue();
29  if (!categoryDocs.containsKey(component.getCategoryString())) {
30  categoryDocs.put(component.getCategoryString(), new StringBuilder());
31  }
32  if (!sortedComponents.containsKey(component.getCategoryString())) {
33  sortedComponents.put(component.getCategoryString(), new TreeMap<String, ComponentInfo>());
34  }
35  categoryNames.put(component.getCategoryString(), component.getCategory());
36  sortedComponents.get(component.getCategoryString()).put(component.displayName, component);
37  }
38 
39  for (String category : categoryDocs.keySet()) {
40  try (BufferedWriter out =
41  new BufferedWriter(getOutputWriter(category.toLowerCase() + ".md"))) {
42  out.write("---\nlayout: documentation\ntitle: ");
43  out.write(categoryNames.get(category));
44  out.write("\n---\n\n");
45  out.write("[&laquo; Back to index](index.html)\n");
46  out.write("# ");
47  out.write(categoryNames.get(category));
48  out.write("\n\nTable of Contents:\n");
49  for (Map.Entry<String, ComponentInfo> entry : sortedComponents.get(category).entrySet()) {
50  out.write("\n* [");
51  out.write(entry.getKey());
52  out.write("](");
53  out.write("#");
54  out.write(entry.getKey());
55  out.write(")");
56  }
57  for (Map.Entry<String, ComponentInfo> entry : sortedComponents.get(category).entrySet()) {
58  String name = entry.getKey();
59  out.write("\n\n## ");
60  out.write(name);
61  out.write(" {#" + name + "}\n\n");
62  ComponentInfo info = entry.getValue();
63  out.write(info.getLongDescription(info));
64  out.write("\n\n");
65  outputProperties(name, info, out);
66  outputEvents(name, info, out);
67  outputMethods(name, info, out);
68  }
69  out.write("\n");
70  }
71  }
72  }
73 
74  private void outputProperties(String name, ComponentInfo info, Writer out) throws IOException {
75  out.write(String.format("%n%n### Properties {#%s-Properties}%n%n{:.properties}", name));
76  boolean didOutput = false;
77  for (Property property : info.properties.values()) {
78  if (isPropertyHidden(info, property)) {
79  continue;
80  }
81  out.write(String.format("%n%n{:id=\"%s.%s\" %s} *%s*%n: ", name, property.name,
82  getPropertyClass(info, property), property.name));
83  out.write(property.getLongDescription(info));
84  didOutput = true;
85  }
86  if (!didOutput) {
87  out.write("\nNone\n");
88  }
89  }
90 
91  private boolean isPropertyHidden(ComponentInfo info, Property property) {
92  return (isFeatureHidden(info, property) && !info.designerProperties.containsKey(property.name))
93  || (info.displayName.equals("Screen") && property.name.equals("ActionBar"))
94  || (info.displayName.equals("FirebaseDB") && property.name.equals("DefaultURL"))
95  || (info.displayName.equals("CloudDB") && property.name.equals("DefaultRedisServer"));
96  }
97 
98  private boolean isFeatureHidden(ComponentInfo info, Feature feature) {
99  return !feature.isUserVisible() || feature.isDeprecated();
100  }
101 
102  private String getPropertyClass(ComponentInfo info, Property property) {
103  String cls = property.isColor() ? COLOR_TYPE : javaTypeToYailType(property.getType());
104  if (!property.isWritable()) {
105  cls += " .ro";
106  }
107  if (!property.isReadable()) {
108  cls += " .wo";
109  }
110  if (!info.designerProperties.containsKey(property.name)) {
111  cls += " .bo";
112  }
113  if (!property.isUserVisible() && info.designerProperties.containsKey(property.name)) {
114  cls += " .do";
115  }
116  return "." + cls;
117  }
118 
119  private void outputEvents(String name, ComponentInfo info, Writer out) throws IOException {
120  out.write(String.format("%n%n### Events {#%s-Events}%n%n{:.events}", name));
121  boolean didOutput = false;
122  for (Event event : info.events.values()) {
123  if (isFeatureHidden(info, event)) {
124  continue;
125  }
126  out.write(String.format("%n%n{:id=\"%s.%s\"} %s(%s)%n: ", name, event.name, event.name,
127  formatParameters(event.parameters)));
128  out.write(event.getLongDescription(info));
129  didOutput = true;
130  }
131  if (!didOutput) {
132  out.write("\nNone\n");
133  }
134  }
135 
136  private void outputMethods(String name, ComponentInfo info, Writer out) throws IOException {
137  out.write(String.format("%n%n### Methods {#%s-Methods}%n%n{:.methods}", name));
138  boolean didOutput = false;
139  for (Method method : info.methods.values()) {
140  if (isFeatureHidden(info, method)) {
141  continue;
142  }
143  String returnType = "";
144  if (method.getReturnType() != null) {
145  returnType = method.isColor() ? COLOR_TYPE : javaTypeToYailType(method.getReturnType());
146  returnType = " returns " + returnType;
147  }
148  out.write(String.format("%n%n{:id=\"%s.%s\" class=\"method%s\"} <i/> %s(%s)%n: ", name,
149  method.name, returnType, method.name, formatParameters(method.parameters)));
150  out.write(method.getLongDescription(info));
151  didOutput = true;
152  }
153  if (!didOutput) {
154  out.write("\nNone\n");
155  }
156  }
157 
158  private String formatParameters(Collection<Parameter> parameters) {
159  StringBuilder sb = new StringBuilder();
160  String separator = "";
161  for (Parameter param : parameters) {
162  sb.append(separator);
163  sb.append(String.format("*%s*{:.%s}", param.name,
164  param.color ? COLOR_TYPE : javaTypeToYailType(param.type)));
165  separator = ",";
166  }
167  return sb.toString();
168  }
169 }
com.google.appinventor.components.scripts.MarkdownDocumentationGenerator
Definition: MarkdownDocumentationGenerator.java:16
com.google.appinventor.components.scripts.ComponentProcessor.ComponentInfo.getCategoryString
String getCategoryString()
Definition: ComponentProcessor.java:928
com.google.appinventor.components.scripts.MarkdownDocumentationGenerator.outputResults
void outputResults()
Definition: MarkdownDocumentationGenerator.java:21
com.google.appinventor.components.scripts.ComponentProcessor.ComponentInfo
Definition: ComponentProcessor.java:644
com.google.appinventor.components.scripts.ComponentProcessor.components
final SortedMap< String, ComponentInfo > components
Definition: ComponentProcessor.java:207
com.google.appinventor.components.scripts.ComponentProcessor.ComponentInfo.getCategory
String getCategory()
Definition: ComponentProcessor.java:917
com.google.appinventor.components.scripts.ComponentProcessor
Definition: ComponentProcessor.java:124
com.google.appinventor.components.scripts.ComponentProcessor.ComponentInfo.displayName
final String displayName
Definition: ComponentProcessor.java:765