• R/O
  • SSH
  • HTTPS

amb: Commit


Commit MetaInfo

Revision216 (tree)
Time2022-02-13 05:04:51
Authormateuszviste

Log Message

fname may point to a directory + assuming utf-8 if no unicode.map file is found

Change Summary

Incremental Difference

--- phpamb/trunk/README (revision 215)
+++ phpamb/trunk/README (revision 216)
@@ -1,5 +1,5 @@
11 phpamb - an online PHP AMB viewer
2-Copyright (C) 2020 Mateusz Viste
2+Copyright (C) 2020-2022 Mateusz Viste
33
44 phpamb is a PHP application that reads an AMB book and processes it dynamically
55 into a nicely formatted html output.
@@ -9,6 +9,10 @@
99
1010 === HISTORY ==================================================================
1111
12+ver 20220212:
13+ - fname can point to a directory instead of an amb file
14+ - when no unicode.map file is present, utf-8 encoding is assumed
15+
1216 ver 20201218:
1317 - document's title is unicode-converted as well
1418
--- phpamb/trunk/phpamb.php (revision 215)
+++ phpamb/trunk/phpamb.php (revision 216)
@@ -1,7 +1,7 @@
11 <?php
22 // php reader of AMB files -- turns an AMB book into a web page
33 //
4-// Copyright (C) 2020 Mateusz Viste
4+// Copyright (C) 2020-2022 Mateusz Viste
55 // http://amb.osdn.io
66 //
77 // MIT license
@@ -24,10 +24,14 @@
2424 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525 // SOFTWARE.
2626
27-$VERSION = "20201218";
27+$VERSION = "20220212";
2828
2929 function getamafile($ambfname, $amafname) {
30- if (! is_file($ambfname)) return(FALSE);
30+ if (! is_file($ambfname)) {
31+ // if its a flat dir, just load the file
32+ if (is_dir($ambfname)) return(file_get_contents($ambfname . '/' . $amafname));
33+ return(FALSE);
34+ }
3135 $fd = fopen($ambfname, "rb");
3236 if ($fd === FALSE) return(FALSE);
3337 // read header (AMB1)
@@ -87,23 +91,23 @@
8791 for ($i = 0; $i < 128; $i++) $unicodemap[$i] = $i; // low ascii is the same
8892
8993 $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] . ';';
94+if ($unicodemaptemp === FALSE) {
95+ $unicodemap = FALSE;
96+} else {
97+ $unicodemap = array_merge($unicodemap, $unicodemaptemp);
98+ /* convert the unicode map so it contains actual html code instead of glyph values */
99+ for ($i = 0; $i < 256; $i++) {
100+ if ($unicodemap[$i] < 128) {
101+ $unicodemap[$i] = htmlspecialchars(chr($unicodemap[$i]), ENT_HTML5);
102+ } else {
103+ $unicodemap[$i] = '&#' . $unicodemap[$i] . ';';
104+ }
99105 }
106+ // perform UTF-8 conversion of the title
107+ $title = txttoutf8($title, $unicodemap);
100108 }
101109
102110
103-// perform UTF-8 conversion of the title
104-$title = txttoutf8($title, $unicodemap);
105-
106-
107111 echo "<!DOCTYPE html>\n";
108112 echo "<html>\n";
109113 echo "<head>\n";
@@ -120,7 +124,11 @@
120124 /* detect links first, before any htmlization occurs */
121125 $ama = preg_replace('!(https?|ftp)://([-A-Z0-9./_*?&;%=#~:]+)!i', 'LiNkStArTxXx$0LiNkEnDxXx', $ama);
122126
123-$amacontent = str_split($ama, 1);
127+if ($unicodemap !== FALSE) {
128+ $amacontent = str_split($ama, 1);
129+} else {
130+ $amacontent = mb_str_split($ama, 1, 'utf-8');
131+}
124132 $escnow = 0; // next char is an escape code
125133 $readlink = 0; // a link target is being read
126134 $opentag = ''; // do I have a currently open html tag?
@@ -170,7 +178,11 @@
170178 if ($c == '%') {
171179 $escnow = 1;
172180 } else {
173- $out .= $unicodemap[ord($c)]; // convert characters into HTML unicode codes
181+ if ($unicodemap !== FALSE) {
182+ $out .= $unicodemap[ord($c)]; // convert characters into HTML unicode codes
183+ } else {
184+ $out .= $c;
185+ }
174186 }
175187 }
176188
Show on old repository browser