blob: d4502249178b518712baf60b7d50fabb9c6af571 [file] [log] [blame]
Ben Dodson920dbbb2010-08-04 15:21:06 -07001/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.doclava;
18
19import com.google.clearsilver.jsilver.data.Data;
20
21import java.io.*;
22import java.util.regex.Pattern;
23import java.util.regex.Matcher;
24
25
26public class DocFile {
27 private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
28 private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
29
30 public static String readFile(String filename) {
31 try {
32 File f = new File(filename);
33 int length = (int) f.length();
34 FileInputStream is = new FileInputStream(f);
35 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
36 char[] buf = new char[length];
37 int index = 0;
38 int amt;
39 while (true) {
40 amt = reader.read(buf, index, length - index);
41
42 if (amt < 1) {
43 break;
44 }
45
46 index += amt;
47 }
48 return new String(buf, 0, index);
49 } catch (IOException e) {
50 return null;
51 }
52 }
53
54 public static void writePage(String docfile, String relative, String outfile) {
55 Data hdf = Doclava.makeHDF();
56
57 /*
58 * System.out.println("docfile='" + docfile + "' relative='" + relative + "'" + "' outfile='" +
59 * outfile + "'");
60 */
61
62 String filedata = readFile(docfile);
63
64 // The document is properties up until the line "@jd:body".
65 // Any blank lines are ignored.
66 int start = -1;
67 int lineno = 1;
68 Matcher lines = LINE.matcher(filedata);
69 String line = null;
70 while (lines.find()) {
71 line = lines.group(1);
72 if (line.length() > 0) {
73 if (line.equals("@jd:body")) {
74 start = lines.end();
75 break;
76 }
77 Matcher prop = PROP.matcher(line);
78 if (prop.matches()) {
79 String key = prop.group(1);
80 String value = prop.group(2);
81 hdf.setValue(key, value);
82 } else {
83 break;
84 }
85 }
86 lineno++;
87 }
88 if (start < 0) {
89 System.err.println(docfile + ":" + lineno + ": error parsing docfile");
90 if (line != null) {
91 System.err.println(docfile + ":" + lineno + ":" + line);
92 }
93 System.exit(1);
94 }
95
96 // if they asked to only be for a certain template, maybe skip it
97 String fromTemplate = hdf.getValue("template.which", "");
98 String fromPage = hdf.getValue("page.onlyfortemplate", "");
99 if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
100 return;
101 }
102
103 // and the actual text after that
104 String commentText = filedata.substring(start);
105
106 Comment comment = new Comment(commentText, null, new SourcePositionInfo(docfile, lineno, 1));
107 TagInfo[] tags = comment.tags();
108
109 TagInfo.makeHDF(hdf, "root.descr", tags);
110
111 hdf.setValue("commentText", commentText);
112
113 // write the page using the appropriate root template, based on the
114 // whichdoc value supplied by build
115 String fromWhichmodule = hdf.getValue("android.whichmodule", "");
116 if (fromWhichmodule.equals("online-pdk")) {
117 // leaving this in just for temporary compatibility with pdk doc
118 hdf.setValue("online-pdk", "true");
119 // add any conditional login for root template here (such as
120 // for custom left nav based on tab etc.
121 ClearPage.write(hdf, "docpage.cs", outfile);
122 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700123 String filename = outfile;
124 // Check whether this is a localized page and remove the intl/*/ path from filename
125 if (filename.indexOf("intl/") == 0) {
126 filename = filename.substring(filename.indexOf("/", 5) + 1); // After intl/ to get 2nd /
127 }
128 if (filename.indexOf("design/") == 0) {
Roman Nurik33527062012-03-06 12:24:35 -0800129 hdf.setValue("design", "true");
Scott Main3c925b42012-06-21 20:35:04 -0700130 } else if (filename.indexOf("develop/") == 0) {
131 hdf.setValue("develop", "true");
132 } else if (filename.indexOf("guide/") == 0) {
133 hdf.setValue("guide", "true");
134 } else if (filename.indexOf("training/") == 0) {
135 hdf.setValue("training", "true");
136 } else if (filename.indexOf("more/") == 0) {
137 hdf.setValue("more", "true");
138 } else if (filename.indexOf("google/") == 0) {
139 hdf.setValue("google", "true");
140 } else if (filename.indexOf("distribute/") == 0) {
141 hdf.setValue("distribute", "true");
142 } else if (filename.indexOf("about/") == 0) {
143 hdf.setValue("about", "true");
144 } else if ((filename.indexOf("tools/") == 0) || (filename.indexOf("sdk/") == 0)) {
145 hdf.setValue("tools", "true");
146 }
147
148 if ((filename.indexOf("tools/sdk/preview/index.html") == 0) ||
149 (filename.indexOf("sdk/index.html") == 0) ||
150 (filename.indexOf("tools/sdk/ndk/index.html") == 0)) {
151 ClearPage.write(hdf, "sdkpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700152 } else {
Scott Main3c925b42012-06-21 20:35:04 -0700153 ClearPage.write(hdf, "docpage.cs", outfile);
Ben Dodson920dbbb2010-08-04 15:21:06 -0700154 }
155 }
156 } // writePage
157}