• R/O
  • SSH
  • HTTPS

amb: Commit


Commit MetaInfo

Revision163 (tree)
Time2020-12-19 00:10:32
Authormateuszviste

Log Message

freezed ver 20201218 to tags

Change Summary

Incremental Difference

--- phpamb/tags/phpamb-20201218/README (nonexistent)
+++ phpamb/tags/phpamb-20201218/README (revision 163)
@@ -0,0 +1,53 @@
1+phpamb - an online PHP AMB viewer
2+Copyright (C) 2020 Mateusz Viste
3+
4+phpamb is a PHP application that reads an AMB book and processes it dynamically
5+into a nicely formatted html output.
6+
7+homepage: http://amb.osdn.io
8+
9+
10+=== HISTORY ==================================================================
11+
12+ver 20201218:
13+ - document's title is unicode-converted as well
14+
15+ver 20201215:
16+ - links enclosed in <> brackets are properly processed
17+
18+ver 20201212:
19+ - implemented AMB 'notice' and 'boring' attributes
20+ - properly closing any open tag when end of document is reached
21+
22+ver 20201210:
23+ - special chars are converted to html entities only when strictly required
24+ - filenames in title bar are displayed without extension
25+ - links are built as relative queries (suggested by Jerome Shidel)
26+ - top title is clickable and directs to index (suggested by Robert Riebisch)
27+ - in-text URLs are detected and linkified (suggested by Robert Riebisch)
28+ - implemented unicode.map encoding to output unicode text when possible
29+ - filenames are processed in a case-insensitive way
30+
31+ver 20201207:
32+ - first public release
33+
34+
35+=== MIT LICENSE ==============================================================
36+
37+Permission is hereby granted, free of charge, to any person obtaining a copy
38+of this software and associated documentation files (the "Software"), to deal
39+in the Software without restriction, including without limitation the rights
40+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
41+copies of the Software, and to permit persons to whom the Software is
42+furnished to do so, subject to the following conditions:
43+
44+The above copyright notice and this permission notice shall be included in all
45+copies or substantial portions of the Software.
46+
47+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
50+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
51+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
52+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53+SOFTWARE.
--- phpamb/tags/phpamb-20201218/phpamb.css (nonexistent)
+++ phpamb/tags/phpamb-20201218/phpamb.css (revision 163)
@@ -0,0 +1,64 @@
1+@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap');
2+
3+html {
4+ background-color: #eee;
5+}
6+
7+body {
8+ font-size: 1.2em;
9+ font-family: 'Roboto Mono', monospace;
10+ width: 78ch;
11+ background-color: #fff;
12+ margin: 0 auto;
13+ padding: 0.5em;
14+ padding-top: 0;
15+ white-space: pre;
16+ border: 1px #aaa solid;
17+}
18+
19+h1 {
20+ font-size: 1em;
21+ font-weight: bold;
22+ display: inline;
23+}
24+
25+a {
26+ color: #00f;
27+ text-decoration: none;
28+}
29+
30+a:hover {
31+ color: #fff;
32+ background-color: #44f;
33+}
34+
35+a.liketext, a.liketext:hover {
36+ color: inherit;
37+ background-color: inherit;
38+ font-size: inherit;
39+ font-weight: inherit;
40+ text-decoration: inherit;
41+}
42+
43+div {
44+ margin: 0 -0.5em;
45+ padding: 0 0.5em;
46+ display: flex;
47+ justify-content: space-between;
48+ background-color: #666;
49+}
50+
51+span {
52+ color: #eee;
53+ margin: 0;
54+ padding: 0;
55+}
56+
57+span.notice {
58+ color: #330;
59+ background-color: #f70;
60+}
61+
62+span.boring {
63+ color: #888;
64+}
--- phpamb/tags/phpamb-20201218/phpamb.php (nonexistent)
+++ phpamb/tags/phpamb-20201218/phpamb.php (revision 163)
@@ -0,0 +1,192 @@
1+<?php
2+// php reader of AMB files -- turns an AMB book into a web page
3+//
4+// Copyright (C) 2020 Mateusz Viste
5+// http://amb.osdn.io
6+//
7+// MIT license
8+//
9+// Permission is hereby granted, free of charge, to any person obtaining a copy
10+// of this software and associated documentation files (the "Software"), to deal
11+// in the Software without restriction, including without limitation the rights
12+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+// copies of the Software, and to permit persons to whom the Software is
14+// furnished to do so, subject to the following conditions:
15+//
16+// The above copyright notice and this permission notice shall be included in all
17+// copies or substantial portions of the Software.
18+//
19+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+// SOFTWARE.
26+
27+$VERSION = "20201218";
28+
29+function getamafile($ambfname, $amafname) {
30+ if (! is_file($ambfname)) return(FALSE);
31+ $fd = fopen($ambfname, "rb");
32+ if ($fd === FALSE) return(FALSE);
33+ // read header (AMB1)
34+ if (fread($fd, 4) !== 'AMB1') return(FALSE);
35+ // read number of ama files inside
36+ $fcount = reset(unpack('v', fread($fd, 2)));
37+ if ($fcount === FALSE) return(FALSE);
38+ // read index until AMA file is found
39+ $offset = 0;
40+ $amalen = 0;
41+ $amafile = '';
42+ for ($i = 0; $i < $fcount; $i++) {
43+ $amafile = rtrim(fread($fd, 12), "\0");
44+ $offset = reset(unpack('V', fread($fd, 4)));
45+ $amalen = reset(unpack('v', fread($fd, 2)));
46+ $bsum = reset(unpack('v', fread($fd, 2)));
47+ if (strcasecmp($amafile, $amafname) == 0) break;
48+ }
49+ if ($i >= $fcount) return(FALSE); // not found
50+ // jump to offset and read file
51+ fseek($fd, $offset);
52+ $result = fread($fd, $amalen);
53+ fclose($fd);
54+ return($result);
55+}
56+
57+
58+// converts str into utf-8 using the unicodemap lookup table and returns the resulting (converted) str
59+function txttoutf8($str, $unicodemap) {
60+ $s = str_split($str, 1); // convert the string to a table for
61+ $res = '';
62+ foreach ($s as $c) $res .= $unicodemap[ord($c)]; // convert raw characters into HTML unicode codes
63+ return($res);
64+}
65+
66+
67+// MAIN STARTS HERE
68+
69+
70+if (empty($_GET['fname'])) {
71+ echo "usage: phpamb.php?fname=file.amb\n";
72+ exit(0);
73+}
74+
75+$ambfname = $_GET['fname'];
76+$f = 'index.ama'; // default file
77+if (! empty($_GET['f'])) $f = $_GET['f'];
78+
79+$title = trim(getamafile($ambfname, 'title'));
80+if ($title === FALSE) $title = $ambfname;
81+
82+$ama = getamafile($ambfname, $f);
83+if ($ama === FALSE) $ama = 'ERROR: FILE NOT FOUND';
84+
85+// prepare a 256-entries lookup array for unicode encoding
86+$unicodemap = array();
87+for ($i = 0; $i < 128; $i++) $unicodemap[$i] = $i; // low ascii is the same
88+
89+$unicodemaptemp = unpack('v128', getamafile($ambfname, 'unicode.map'));
90+if ($unicodemaptemp === FALSE) $unicodemaptemp = array_fill(0, 128, 0xfffd);
91+$unicodemap = array_merge($unicodemap, $unicodemaptemp);
92+
93+/* convert the unicode map so it contains actual html code instead of glyph values */
94+for ($i = 0; $i < 256; $i++) {
95+ if ($unicodemap[$i] < 128) {
96+ $unicodemap[$i] = htmlspecialchars(chr($unicodemap[$i]), ENT_HTML5);
97+ } else {
98+ $unicodemap[$i] = '&#' . $unicodemap[$i] . ';';
99+ }
100+}
101+
102+
103+// perform UTF-8 conversion of the title
104+$title = txttoutf8($title, $unicodemap);
105+
106+
107+echo "<!DOCTYPE html>\n";
108+echo "<html>\n";
109+echo "<head>\n";
110+echo ' <meta charset="UTF-8">' . "\n";
111+echo " <title>{$title}</title>\n";
112+echo ' <link rel="stylesheet" href="phpamb.css">' . "\n";
113+echo ' <meta name="viewport" content="width=device-width, initial-scale=1">' . "\n";
114+echo " <meta name=\"generator\" content=\"phpamb/{$VERSION}\">\n";
115+echo "</head>\n";
116+echo "<body>";
117+
118+echo "<div><span><a href=\"?fname={$ambfname}\" class=\"liketext\">{$title}</a></span><span>[" . pathinfo($f, PATHINFO_FILENAME) . "]</span></div>\n";
119+
120+/* detect links first, before any htmlization occurs */
121+$ama = preg_replace('!(https?|ftp)://([-A-Z0-9./_*?&;%=#~:]+)!i', 'LiNkStArTxXx$0LiNkEnDxXx', $ama);
122+
123+$amacontent = str_split($ama, 1);
124+$escnow = 0; // next char is an escape code
125+$readlink = 0; // a link target is being read
126+$opentag = ''; // do I have a currently open html tag?
127+
128+$out = '';
129+foreach ($amacontent as $c) {
130+ // ignore CR
131+ if ($c == "\r") continue;
132+ // is link target being read?
133+ if ($readlink != 0) {
134+ if (($c == "\n") || ($c == ':')) {
135+ $out .= '">';
136+ $opentag = 'a';
137+ $readlink = 0;
138+ if ($c == ':') continue;
139+ } else {
140+ $out .= urlencode($c);
141+ }
142+ continue;
143+ }
144+ //
145+ if ($escnow != 0) {
146+ if ($c == '%') {
147+ $out .= '%';
148+ } else if ($c == 'l') {
149+ $out .= '<a href="' . $_SERVER['PHP_SELF'] . "?fname={$ambfname}&amp;f=";
150+ $readlink = 1;
151+ } else if ($c == 'h') {
152+ $out .= '<h1>';
153+ $opentag = 'h1';
154+ } else if ($c == '!') {
155+ $out .= '<span class="notice">';
156+ $opentag = 'span';
157+ } else if ($c == 'b') {
158+ $out .= '<span class="boring">';
159+ $opentag = 'span';
160+ }
161+ $escnow = 0;
162+ continue;
163+ }
164+ // close </a> if open and got LF or new tag
165+ if ((!empty($opentag)) && (($c == "\n") || ($c == "%"))) {
166+ $out .= "</{$opentag}>";
167+ $opentag = '';
168+ }
169+ //
170+ if ($c == '%') {
171+ $escnow = 1;
172+ } else {
173+ $out .= $unicodemap[ord($c)]; // convert characters into HTML unicode codes
174+ }
175+}
176+
177+// close open tags
178+if (!empty($opentag)) {
179+ $out .= "</{$opentag}>";
180+ $opentag = '';
181+}
182+
183+/* postprocessing: find links detected earlier and change them to proper anchors */
184+$out = preg_replace('/LiNkStArTxXx.*LiNkEnDxXx/', '<a href="$0">$0</a>', $out);
185+$out = preg_replace('/(LiNkStArTxXx)|(LiNkEnDxXx)/', '', $out);
186+
187+echo $out;
188+
189+echo "</body>\n";
190+echo "</html>\n";
191+
192+?>
Show on old repository browser