DCL 3.7.4
Loading...
Searching...
No Matches
HtmlGenerator.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#ifdef __WINNT__
4#include <windows.h>
5#endif
6
7#include <dcl/Charset.h>
8#include <dcl/Files.h>
9#include <dcl/IniFile.h>
10
11#include "DocParser.h"
12#include "HtmlGenerator.h"
13
14#if __DCL_HAVE_THIS_FILE__
15#undef __THIS_FILE__
16static const char_t __THIS_FILE__[] = __T("src2html/HtmlGenerator.cpp");
17#endif
18
19__DCL_BEGIN_NAMESPACE
20
21HtmlGenerator::HtmlGenerator(const wchar_t* pszIDocIniFileName)
22{
23 __DCL_ASSERT(pszIDocIniFileName && Files::exists(pszIDocIniFileName));
24// __strIniFileName = pszIDocIniFileName;
25 IniFile ini(pszIDocIniFileName);
26 String strTemp = ini.getString(L"MAIN", L"SYNTAXES");
27 __DCL_ASSERT(!strTemp.isEmpty());
28 StringArray list;
29 strTemp.split(L' ', list);
30
31 String strPath = Files::dirname(pszIDocIniFileName);
32 String strTitle;
33 String strExt;
34 String strSyntaxFile;
35 for(StringArray::Iterator it = list.begin(); it != list.end(); it++) {
36 strTitle = *it;
37 strExt = ini.getString(strTitle, L"FILE_EXTENSION");
38 strSyntaxFile = strPath + ini.getString(strTitle, L"SYNTAX_FILE");
39
40 __DCL_TRACE3(L"Syntax title[%ls] ext[%ls] filename[%ls]",
41 strTitle.data(), strExt.data(), strSyntaxFile.data());
42
43 DocSyntax* p = new DocSyntax(strTitle, strExt);
44 p->load(strSyntaxFile);
45 __vSyntaxes.add(p);
46 }
47
48 __nTabWidth = 4;
49 __bShowLineNumber = true;
50 __nLineNumber = 1;
51}
52
54{
55 if (!__vSyntaxes.isEmpty()) {
56 for(size_t i = 0; i < __vSyntaxes.size(); i++)
57 delete (DocSyntax*)__vSyntaxes[i];
58 }
59}
60
61#ifdef __GNUC__
62#define _snprintf snprintf
63#endif
64
66{
68
69 out << String::format(L"\n<span style=\"color:gray;\">%3d: ", __nLineNumber++);
70 out << L"</span>";
71
73}
74
76{
77 switch(type) {
78 case normal :
79 break;
80 case comment :
81 out << L"<span style=\"color:green;\">";
82 break;
83 case quotation :
84 out << L"<span style=\"color:red;\">";
85 break;
86 case keyword :
87 out << L"<span style=\"color:blue;\">";
88 break;
89 default :
90 __DCL_ASSERT(false);
91 }
92}
93
95{
96 switch(type) {
97 case normal :
98 break;
99 case comment :
100 case quotation :
101 case keyword :
102 out << L"</span>";
103 break;
104 default :
105 __DCL_ASSERT(false);
106 }
107}
108
109#define FLUSH_PRIOR \
110 { \
111 if (pStart < pCurrent) \
112 out.write(pStart, pCurrent - pStart); \
113 pStart = pCurrent + 1; \
114 }
115
116/*
117 if (pCurrent > pStart)
118 out->write(pStart, pCurrent - pStart);
119 pStart = pCurrent + 1;
120*/
122 Writer& out, const wchar_t* pData, size_t nDataSize
123)
124{
125 const wchar_t* pStart = pData;
126 const wchar_t* pCurrent = pStart;
127
128 while(pCurrent < (pData + nDataSize)) {
129 switch(*pCurrent) {
130 case L'\t': {
131 if (__nTabWidth > 0) {
133 String str(L' ', __nTabWidth);
134 out.write(str.data(), str.length());
135 }
136 break;
137 }
138 case L'\r': {
139 if (__bShowLineNumber) {
141 }
142 break;
143 }
144 case L'\n': {
145 if (__bShowLineNumber) {
147 writeLineNumber(out);
148 }
149 else
150 out << L"\n";
151
152 break;
153 }
154 case '>': {
156 out << L"&gt;";
157 break;
158 }
159 case '<': {
161 out << L"&lt;";
162 break;
163 }
164 case '&': {
166 out << L"&amp;";
167 }
168 default: {
169 // out->write(pCurrent, 1);
170 ;
171 }
172 }
173 pCurrent++;
174 }
176}
177
179 Writer& out,
180 const wchar_t* pData,
181 size_t nDataSize,
182 const wchar_t* pszLanguageTitle
183)
184{
186 DocSyntax* pSyntax = NULL;
187 for(size_t i = 0; i < __vSyntaxes.size(); i++) {
188 if (((DocSyntax*)__vSyntaxes[i])->isValidLanguage(pszLanguageTitle))
189 {
190 pSyntax = (DocSyntax*)__vSyntaxes[i];
191 break;
192 }
193 }
194 __DCL_ASSERT(pSyntax != NULL); // Not Found Syntax
195
196 if (__nTabWidth == 0)
197 __nTabWidth = pSyntax->tabWidth();
198
199 out << L"\n<pre style=\"background-color:white; color:black; font-style=normal; font-weight:normal\">";
200 __nLineNumber = 1;
202 writeLineNumber(out);
203
204 DocParser parser(pData, nDataSize, pSyntax);
205 DOC_BLOCK block;
206 while(parser.getNextBlock(block)) {
207 if (__currentType != block.type) {
209 writeBegin(out, block.type);
210 __currentType = block.type;
211 }
212
213 writeData(out, block.pData, block.nDataSize);
214 }
216 out << L"\n</pre>";
217}
218
220 Writer& out,
221 const wchar_t* pszFileName,
222 const wchar_t* pszLangSyntax // = NULL
223)
224{
225 __DCL_ASSERT(pszFileName != NULL);
226
227 String strLangSyntax;
228 if (pszLangSyntax == NULL) {
229 const wchar_t* p = pszFileName + String::length(pszFileName);
230 while(--p > pszFileName) {
231 if (*p == '.') {
232 p++;
233 break;
234 }
235 }
236
237 if (p == pszFileName)
238 return false; // invalid file name;
239
240 DocSyntax* pSyntax = NULL;
241 for(size_t i = 0; i < __vSyntaxes.size(); i++) {
242 pSyntax = (DocSyntax*)(__vSyntaxes[i]);
243 if (pSyntax->isValidExtension(p)) {
244 strLangSyntax = pSyntax->languageTitle();
245 pszLangSyntax = strLangSyntax.data();
246 break;
247 }
248 }
249 }
250
251 __DCL_ASSERT(pszLangSyntax != NULL);
252 __DCL_ASSERT(Files::exists(pszFileName));
253
254 UTF8Decoder dec;
255 String text = Files::readText(pszFileName, dec);
256
258 out,
259 text,
260 text.length(),
261 pszLangSyntax
262 );
263 return true;
264}
265
267 Writer& out,
268 const wchar_t* pData,
269 size_t nDataSize,
270 const wchar_t* pszLangSyntax
271)
272{
274 out,
275 pData,
276 nDataSize,
277 pszLangSyntax
278 );
279 return true;
280}
281
282void HtmlGenerator::setOption(int nTabWidth, bool bShowLineNumber)
283{
284 __nTabWidth = nTabWidth;
285 __bShowLineNumber = bShowLineNumber;
286}
287
289 Writer& out,
290 const wchar_t* pszHtmlTitle
291)
292{
293 if (pszHtmlTitle == NULL)
294 pszHtmlTitle = L"Unknown File Name";
295
296 StringBuilder strHeader = L""
297 "<!DOCTYPE html>"
298 "\n<html lang=\"ko-KR\">"
299 "\n<head>"
300 "\n<meta charset=\"UTF-8\" >"
301 "\n<meta name=\"generator\" content=\"src2html 0.9\">"
302 "\n<meta name=\"author\" content=\"Daejung Kim &lt;daejung@sysdeveloper.net&gt; http://www.sysdeveloper.net/daejung\">"
303 "\n<title>";
304 strHeader += pszHtmlTitle;
305 strHeader += L"</title>";
306 strHeader += L""
307 "\n</head>"
308 "\n<body>";
309
310 out << strHeader;
311}
312
314{
315 String strFooter = L""
316 "\n</body>"
317 "\n</html>";
318
319 out << strFooter;
320}
321
322__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
wchar_t char_t
Definition Config.h:247
BlockType
Definition DocParser.h:15
@ quotation
Definition DocParser.h:18
@ comment
Definition DocParser.h:17
@ keyword
Definition DocParser.h:19
@ normal
Definition DocParser.h:16
#define FLUSH_PRIOR
#define __DCL_TRACE3(fmt, arg1, arg2, arg3)
Definition Object.h:401
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define __T(str)
Definition Object.h:60
bool getNextBlock(DOC_BLOCK &block)
Definition DocParser.cpp:32
int tabWidth() const
Definition DocSyntax.h:89
bool load(const wchar_t *pszFileName)
Definition DocSyntax.cpp:36
const String & languageTitle() const
Definition DocSyntax.h:79
bool isValidExtension(const wchar_t *pszFileExt) const
Definition DocSyntax.cpp:67
static String dirname(const String &_path)
Definition Files.cpp:268
static bool exists(const String &_path)
Definition Files.cpp:109
static String readText(const String &_filename) __DCL_THROWS1(IOException *)
Definition Files.cpp:435
void generateHelper(Writer &out, const wchar_t *pData, size_t nDataSize, const wchar_t *pszLanguageTitle)
virtual ~HtmlGenerator()
void setOption(int nTabWidth, bool bShowLineNumber)
HtmlGenerator(const wchar_t *pszIDocIniFileName)
void writeBegin(Writer &out, BlockType type)
void generateHeader(Writer &out, const wchar_t *pszHtmlTitle=NULL)
void writeLineNumber(Writer &out)
bool generate(Writer &out, const wchar_t *pszFileName, const wchar_t *pszLangSyntax=NULL)
void writeData(Writer &out, const wchar_t *pData, size_t nDataSize)
void writeEnd(Writer &out, BlockType type)
PointerArray __vSyntaxes
void generateFooter(Writer &out)
BlockType __currentType
BlockType type
Definition DocParser.h:26
const wchar_t * pData
Definition DocParser.h:24
size_t nDataSize
Definition DocParser.h:25