1 // common/html.cc
2 // This file is part of Anyterm; see http://anyterm.org/
3 // (C) 2005 Philip Endecott
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19
20 #include "html.hh"
21
22 #include <string>
23
24 using namespace std;
25
26
27 // Screen to HTML conversion:
28
gen_style(ucs4_string & h,Attributes attrs)29 static bool gen_style(ucs4_string& h, Attributes attrs)
30 {
31 if (attrs!=Attributes()) {
32 unsigned int fg = attrs.fg;
33 unsigned int bg = attrs.bg;
34 if (attrs.inverse) {
35 swap(fg,bg);
36 }
37 ucs4_string classes;
38 if (attrs.bold) {
39 classes += L'z';
40 }
41 if (bg!=Attributes().bg) {
42 if (!classes.empty()) {
43 classes += L' ';
44 }
45 classes += L'a'+bg;
46 }
47 if (fg!=Attributes().fg) {
48 if (!classes.empty()) {
49 classes += L' ';
50 }
51 classes += L'i'+fg;
52 }
53 h += L"<span class=\"" + classes + L"\">";
54 return true;
55 }
56 return false;
57 }
58
59 static const ucs4_char* attr_end = L"</span>";
60
61 static const ucs4_char* cursor_start = L"<span class=\"cursor\">";
62 static const ucs4_char* cursor_end = L"</span>";
63
64
htmlify_screen(const Screen & screen)65 ucs4_string htmlify_screen(const Screen& screen)
66 {
67 // Convert screen into HTML.
68 // Slightly optimised to reduce spaces at right end of lines.
69
70 ucs4_string h;
71
72 for (int r=-screen.scrollback(); r<screen.rows(); r++) {
73 int sp=0;
74 bool styled=false;
75 Attributes prev_attrs;
76 for (int c=0; c<screen.cols(); c++) {
77 bool cursor = (r==screen.cursor_row && c==screen.cursor_col) && screen.cursor_visible;
78 Cell cell = screen(r,c);
79 ucs4_char ch = cell.c;
80 Attributes attrs = cell.attrs;
81
82 if (ch==' ' && attrs==Attributes() && !styled && c>0 && r>0 && !cursor) {
83 sp++;
84 } else {
85 while (sp>0) {
86 h+=L'\u00A0';
87 sp--;
88 }
89 if (styled && attrs!=prev_attrs) {
90 h+=attr_end;
91 }
92 if (c==0 || attrs!=prev_attrs) {
93 styled = gen_style(h,attrs);
94 prev_attrs=attrs;
95 }
96 if (cursor) {
97 h+=cursor_start;
98 }
99 switch (ch) {
100 case '<': h+=L"<"; break;
101 case '>': h+=L">"; break;
102 case '&': h+=L"&"; break;
103 case ' ': h+=L'\u00A0'; break;
104 default: h+=ch; break;
105 }
106 if (cursor) {
107 h+=cursor_end;
108 }
109 }
110 }
111 if (styled) {
112 h+=attr_end;
113 }
114 h+=L"<br>";
115 }
116
117 return h;
118 }
119
120
121