]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/html/m_pre.cpp | |
3 | // Purpose: wxHtml module for <PRE> ... </PRE> tag (code citation) | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // Licence: wxWindows licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | #include "wx/wxprec.h" | |
10 | ||
11 | #ifdef __BORLANDC__ | |
12 | #pragma hdrstop | |
13 | #endif | |
14 | ||
15 | #if wxUSE_HTML && wxUSE_STREAMS | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #endif | |
19 | ||
20 | #include "wx/html/forcelnk.h" | |
21 | #include "wx/html/m_templ.h" | |
22 | ||
23 | #include "wx/html/htmlcell.h" | |
24 | #include "wx/tokenzr.h" | |
25 | #include "wx/encconv.h" | |
26 | ||
27 | FORCE_LINK_ME(m_pre) | |
28 | ||
29 | // replaces '\t', ' ' and '\n' with HTML markup: | |
30 | static wxString LINKAGEMODE HtmlizeLinebreaks(const wxString& str) | |
31 | { | |
32 | wxString out; | |
33 | out.reserve(str.length()); // we'll certainly need at least that | |
34 | ||
35 | const wxString::const_iterator end = str.end(); | |
36 | for ( wxString::const_iterator i = str.begin(); i != end; ++i ) | |
37 | { | |
38 | switch ( (*i).GetValue() ) | |
39 | { | |
40 | case '<': | |
41 | while ( i != end && *i != '>' ) | |
42 | { | |
43 | out << *i++; | |
44 | } | |
45 | out << '>'; | |
46 | if ( i == end ) | |
47 | return out; | |
48 | break; | |
49 | ||
50 | // We need to translate any line break into exactly one <br>. | |
51 | // Quoting HTML spec: "A line break is defined to be a carriage | |
52 | // return (
), a line feed (
), or a carriage | |
53 | // return/line feed pair." | |
54 | case '\r': | |
55 | { | |
56 | wxString::const_iterator j(i + 1); | |
57 | if ( j != end && *j == '\n' ) | |
58 | i = j; | |
59 | } | |
60 | // fall through | |
61 | case '\n': | |
62 | out << "<br>"; | |
63 | break; | |
64 | ||
65 | default: | |
66 | out << *i; | |
67 | break; | |
68 | } | |
69 | } | |
70 | ||
71 | return out; | |
72 | } | |
73 | ||
74 | ||
75 | //----------------------------------------------------------------------------- | |
76 | // The list handler: | |
77 | //----------------------------------------------------------------------------- | |
78 | ||
79 | ||
80 | TAG_HANDLER_BEGIN(PRE, "PRE") | |
81 | TAG_HANDLER_CONSTR(PRE) { } | |
82 | ||
83 | TAG_HANDLER_PROC(tag) | |
84 | { | |
85 | wxHtmlContainerCell *c; | |
86 | ||
87 | const int fixed = m_WParser->GetFontFixed(); | |
88 | const int italic = m_WParser->GetFontItalic(); | |
89 | const int underlined = m_WParser->GetFontUnderlined(); | |
90 | const int bold = m_WParser->GetFontBold(); | |
91 | const int fsize = m_WParser->GetFontSize(); | |
92 | const wxHtmlWinParser::WhitespaceMode whitespace = | |
93 | m_WParser->GetWhitespaceMode(); | |
94 | ||
95 | c = m_WParser->GetContainer(); | |
96 | m_WParser->SetWhitespaceMode(wxHtmlWinParser::Whitespace_Pre); | |
97 | m_WParser->SetFontUnderlined(false); | |
98 | m_WParser->SetFontBold(false); | |
99 | m_WParser->SetFontItalic(false); | |
100 | m_WParser->SetFontFixed(true); | |
101 | m_WParser->SetFontSize(3); | |
102 | c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
103 | ||
104 | m_WParser->CloseContainer(); | |
105 | c = m_WParser->OpenContainer(); | |
106 | c->SetWidthFloat(tag); | |
107 | c = m_WParser->OpenContainer(); | |
108 | c->SetAlignHor(wxHTML_ALIGN_LEFT); | |
109 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); | |
110 | ||
111 | wxString srcMid = m_WParser->GetInnerSource(tag); | |
112 | ||
113 | // setting Whitespace_Pre mode takes care of spaces and TABs, but | |
114 | // not linebreaks, so we have to translate them into <br> by | |
115 | // calling HtmlizeLinebreaks() here | |
116 | ParseInnerSource(HtmlizeLinebreaks(srcMid)); | |
117 | ||
118 | m_WParser->CloseContainer(); | |
119 | m_WParser->CloseContainer(); | |
120 | c = m_WParser->OpenContainer(); | |
121 | ||
122 | m_WParser->SetWhitespaceMode(whitespace); | |
123 | m_WParser->SetFontUnderlined(underlined); | |
124 | m_WParser->SetFontBold(bold); | |
125 | m_WParser->SetFontItalic(italic); | |
126 | m_WParser->SetFontFixed(fixed); | |
127 | m_WParser->SetFontSize(fsize); | |
128 | c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
129 | ||
130 | return true; | |
131 | } | |
132 | ||
133 | TAG_HANDLER_END(PRE) | |
134 | ||
135 | ||
136 | ||
137 | ||
138 | ||
139 | TAGS_MODULE_BEGIN(Pre) | |
140 | ||
141 | TAGS_MODULE_ADD(PRE) | |
142 | ||
143 | TAGS_MODULE_END(Pre) | |
144 | ||
145 | #endif |