File: OptionsBuilder.java

package info (click to toggle)
mkgmap 0.0.0%2Bsvn4923-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,068 kB
  • sloc: java: 73,955; xml: 1,719; python: 713; sh: 240; makefile: 147; perl: 31
file content (163 lines) | stat: -rw-r--r-- 4,570 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2019.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 or
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */
package buildoptions;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Convert options.txt to format needed in help file options. 
 * Saves some manual edits to keep both files in sync.
 * Used with ant gen-options-file  
 * @author Mike Baggaley
 *
 */
public class OptionsBuilder {

	public static void main(String[] args) {
		final int indentSize = 4;
		boolean hasNonAscii = false;
		int lineNumber = 0;
		File outputFile = new File(args[0]);
		System.setProperty("line.separator", "\n");
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
			String line;
			String previousLine = "";
			int indent = 0;
			boolean preformatted = false;
			while((line = br.readLine()) != null) {
				++lineNumber;
				if (!line.matches("\\p{ASCII}*")) {
					System.err.println("Line " + lineNumber + " contains one or more non-ASCII characters.\r\n" + line);
					hasNonAscii = true;
				}
				if (preformatted) {
					if (line.trim().compareToIgnoreCase("</pre>") == 0)
						preformatted = false;
					else {
					    bw.write(line);
					    bw.newLine();
					}
				} else {
					line = line.replaceAll("\\s+", " ");
					if (line.length() > 0) {
						if (line.startsWith(";")) {
							line = line.substring(1);
							indent = 0;
							if (!previousLine.isEmpty()) {
							    bw.write(previousLine);
							    bw.newLine();
							    previousLine = "";
							}
						}
						else if (line.charAt(0) == ':') {
							if (!previousLine.isEmpty()) {
							    bw.write(previousLine);
							    bw.newLine();
							    previousLine = "";
							}
							indent = 1;
							line = line.substring(1);
							while (line.charAt(0) == ':') {
								indent++;
								line = line.substring(1);
							}
							if (line.charAt(0) == ';')
								line = line.substring(1);
						}
						else if (line.trim().compareToIgnoreCase("<p>") == 0) {
							if (!previousLine.isEmpty()) {
							    bw.write(previousLine);
							    bw.newLine();
							    previousLine = "";
							}
							line = "";
						    bw.newLine();
						}
						else if (line.trim().compareToIgnoreCase("<pre>") == 0) {
							if (!previousLine.isEmpty()) {
							    bw.write(previousLine);
							    bw.newLine();
							    previousLine = "";
							}
							line = "";
						    preformatted = true;
						}
						line = line.trim();
						if (!previousLine.isEmpty()) {
							if (!line.isEmpty()) {
								previousLine += " " + line;
							}
						} else {
							previousLine = line;
							for (int i = 0; i < indent; i++) {
								for (int j = 0; j < indentSize; j++)
									previousLine = " " + previousLine;
							}
						}
						while (previousLine.length() > 79) {
							line = previousLine.substring(0, 80);
							int lastSpaceIndex = line.lastIndexOf(' ');
							int firstNonSpaceIndex = 0;
							while (firstNonSpaceIndex < 79) {
								if (line.charAt(firstNonSpaceIndex) != ' ')
									break;
								firstNonSpaceIndex++;
							}
							if (lastSpaceIndex > firstNonSpaceIndex) {
								line = line.substring(0, lastSpaceIndex);
								previousLine = previousLine.substring(lastSpaceIndex + 1);
								for (int i = 0; i < indent; i++) {
									for (int j = 0; j < indentSize; j++)
										previousLine = " " + previousLine;
								}
							    bw.write(line);
							    bw.newLine();
							}
							else {
							    bw.write(previousLine);
							    bw.newLine();
							    previousLine = "";
							}
						}
					}
					else {
						indent = 0;
						if (!previousLine.isEmpty()) {
						    bw.write(previousLine);
						    bw.newLine();
						    previousLine = "";
						}
						bw.newLine();
					}
				}
			}
			if (!previousLine.isEmpty()) {
			    bw.write(previousLine);
			    bw.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		if (hasNonAscii) {
			System.exit(-1);
		}
	}

}