Debian Codegraph @

src/dr/xml/MarkdownDocumentationHandler.java

raw symbols SCIP
1 /*
2 * MarkdownDocumentationHandler.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.xml;
27
28 import dr.app.beast.BeastParser;
29 import dr.app.tools.BeastParserDoc;
30
31 import java.io.PrintWriter;
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.Iterator;
35 import java.util.TreeSet;
36
37 /**
38 * A subclass of XMLDocumentationHandler that creates output in Markdown format
39 */
40 public class MarkdownDocumentationHandler extends XMLDocumentationHandler {
41
42 public MarkdownDocumentationHandler(BeastParser parser) {
43 super(parser);
44 }
45
46 public void outputElements(PrintWriter writer) {
47
48 printDocTitle(writer);
49
50 Iterator iterator = parser.getParsers();
51 while (iterator.hasNext()) {
52 XMLObjectParser xmlParser = (XMLObjectParser)iterator.next();
53 writer.println(xmlParser.toMarkdown(this));
54
55 // convert the html into markdown...
56
57 System.out.println(" outputting Markdown for element " + xmlParser.getParserName());
58 }
59
60 }
61
62 private void printDocTitle(PrintWriter writer) {
63
64 Calendar date = Calendar.getInstance();
65 SimpleDateFormat dateformatter = new SimpleDateFormat("'updated on' d MMMM yyyy zzz");
66
67 writer.println("#" + BeastParserDoc.TITLE + "\n");
68
69 if (parser.parsers != null) {
70 if (parser.parsers.equalsIgnoreCase(BeastParser.RELEASE)) {
71 writer.println("Release Version (" + dateformatter.format(date.getTime()) + ")");
72 System.out.println("Release Version");
73 } else if (parser.parsers.equalsIgnoreCase(BeastParser.DEV)) {
74 writer.println("Development Version (" + dateformatter.format(date.getTime()) + ")");
75 System.out.println("Development Version");
76 }
77 }
78
79 writer.println("");
80 writer.println("The following is a list of valid elements in a beast file.");
81 writer.println("");
82
83 }
84
85 /**
86 * Outputs all types that appear as required attributes or elements in an HTML table to the given writer.
87 * @param writer PrintWriter
88 */
89 public void outputTypes(PrintWriter writer) {
90
91 writer.println("## BEAST types");
92 writer.println("");
93 writer.println("The following is a list of generic types that elements represent in a beast file.");
94 writer.println("");
95
96
97 // iterate through the types
98 //Iterator iterator = requiredTypes.iterator();
99 for (Class requiredType : requiredTypes) {
100 if (requiredType != Object.class) {
101
102 String name = ClassComparator.getName(requiredType);
103
104 System.out.println(" outputting md for generic type " + name);
105
106
107 TreeSet<String> matchingParserNames = new TreeSet<String>();
108
109 // find all parsers that match this required type
110 Iterator i = parser.getParsers();
111 while (i.hasNext()) {
112 XMLObjectParser xmlParser = (XMLObjectParser) i.next();
113 Class returnType = xmlParser.getReturnType();
114 if (returnType != null && requiredType.isAssignableFrom(returnType)) {
115 matchingParserNames.add(xmlParser.getParserName());
116 }
117 }
118
119 if (!(matchingParserNames.size() == 1 && matchingParserNames.iterator().next().equals(name))) {
120
121 // output table row containing the type and the matching parser names
122 writer.println("### " + name + "");
123 writer.println();
124 writer.println("Elements of this type include:");
125 writer.println();
126 i = matchingParserNames.iterator();
127 while (i.hasNext()) {
128 String parserName = (String) i.next();
129 writer.println("- " + getLink(parserName));
130 }
131 writer.println();
132 }
133 }
134
135 }
136 }
137
138 public String getHTMLForClass(Class c) {
139 return getLink(ClassComparator.getName(c));
140 }
141
142 public String getLink(String name) {
143 if (Character.isUpperCase(name.charAt(0))) {
144 // linking to a 'type'
145 return "[" + name + "](#" + name.toLowerCase() + ")";
146 } else {
147 // linking to an 'element'
148 return "[&lt;" + name + "&gt; element](#codelt" + name.toLowerCase() + "gtcode-element)";
149 }
150 }
151 }