1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-09-29 23:10:52 +00:00

Mw_parse: Add initial implementation for thumbnails

This commit is contained in:
gnosygnu 2017-02-05 19:46:25 -05:00
parent 8011f9e979
commit b30f10b647
10 changed files with 218 additions and 80 deletions

View File

@ -74,8 +74,8 @@ public class Gftest {
public static void Eq__bry(byte[] expd, byte[] actl, String msg_fmt, Object... msg_args) {
if (Bry_.Eq(expd, actl)) return;
Write_fail_head(bfr, msg_fmt, msg_args);
bfr.Add_str_a7("expd: ").Add(expd).Add_byte_nl();
bfr.Add_str_a7("actl: ").Add(actl).Add_byte_nl();
bfr.Add_str_a7("expd: ").Add_safe(expd).Add_byte_nl();
bfr.Add_str_a7("actl: ").Add_safe(actl).Add_byte_nl();
bfr.Add(Bry__line_end);
throw Err_.new_wo_type(bfr.To_str_and_clear());
}

View File

@ -44,21 +44,20 @@ public class Xomw_MagicWordArray {
for (int i = 0; i < synonyms_len; i++) {
Xomw_MagicWordSynonym synonym = synonyms[i];
switch (synonym.arg1_tid) {
case Xomw_MagicWordSynonym.Arg1__nil:
case Xomw_MagicWordSynonym.Arg1__end:
if (fwd_trie == null) fwd_trie = word.case_match ? Btrie_slim_mgr.cs() : Btrie_slim_mgr.ci_u8();
fwd_trie.Add_obj(synonym.text, synonym);
fwd_trie.Add_obj(synonym.text_wo_arg1, synonym);
break;
case Xomw_MagicWordSynonym.Arg1__bgn:
if (bwd_trie == null) bwd_trie = Btrie_bwd_mgr.c__(word.case_match);
bwd_trie.Add(synonym.text, synonym);
bwd_trie.Add(synonym.text_wo_arg1, synonym);
break;
// ignore if mid / mix
case Xomw_MagicWordSynonym.Arg1__mid:
case Xomw_MagicWordSynonym.Arg1__mix:
Gfo_usr_dlg_.Instance.Warn_many("", "", "MagicWordArray: unsupported arg_1_tid: tid=~{0}", synonym.arg1_tid);
continue;
case Xomw_MagicWordSynonym.Arg1__nil:
break;
}
}
}
@ -253,7 +252,7 @@ public class Xomw_MagicWordArray {
// check bwd; EX: "$1px"
if (bwd_trie != null) {
Object o = bwd_trie.Match_at(trv, src, src_end, -1);
Object o = bwd_trie.Match_at(trv, src, src_end - 1, -1);
if (o != null) {
return ((Xomw_MagicWordSynonym)o).magic_name;
}

View File

@ -0,0 +1,62 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.mws; import gplx.*; import gplx.xowa.*;
import org.junit.*; import gplx.core.tests.*;
public class Xomw_MagicWordArray__tst {
private final Xomw_MagicWordArray__fxt fxt = new Xomw_MagicWordArray__fxt();
@Test public void Nil() {
fxt.Init__word(Bool_.Y, "nil", "nil");
fxt.Init__ary("nil");
fxt.Test__matchVariableStartToEnd("nil", "nil", "nila");
}
@Test public void Bgn() {
fxt.Init__word(Bool_.Y, "bgn", "bgn$1");
fxt.Init__ary("bgn");
fxt.Test__matchVariableStartToEnd("bgn", "bgn", "bgna");
}
@Test public void End() {
fxt.Init__word(Bool_.Y, "end", "$1end");
fxt.Init__ary("end");
fxt.Test__matchVariableStartToEnd("end", "end", "aend");
}
@Test public void Smoke() {
fxt.Init__word(Bool_.Y, "img_upright", "upright", "upright=$1", "upright $1");
fxt.Init__word(Bool_.Y, "img_width", "$1px");
fxt.Init__ary("img_upright", "img_width");
fxt.Test__matchVariableStartToEnd("img_upright", "upright", "upright=123", "upright 123");
fxt.Test__matchVariableStartToEnd("img_width", "123px");
}
}
class Xomw_MagicWordArray__fxt {
private final Xomw_MagicWordMgr magic_word_mgr = new Xomw_MagicWordMgr();
private Xomw_MagicWordArray magic_word_ary;
public void Init__word(boolean cs, String word, String... synonyms) {
magic_word_mgr.Add(Bry_.new_u8(word), cs, Bry_.Ary(synonyms));
}
public void Init__ary(String... words) {
magic_word_ary = new Xomw_MagicWordArray(magic_word_mgr, Bry_.Ary(words));
}
public void Test__matchVariableStartToEnd(String expd_str, String... vals) {
byte[] expd = Bry_.new_u8(expd_str);
for (String val : vals) {
byte[] actl = magic_word_ary.matchVariableStartToEnd(Bry_.new_u8(val));
Gftest.Eq__bry(expd, actl, val);
}
}
}

View File

@ -17,7 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.mws; import gplx.*; import gplx.xowa.*;
public class Xomw_MagicWordMgr {
private final Hash_adp_bry hash = Hash_adp_bry.cs();
public void Add(byte[] name, boolean cs, byte[]... synonyms) {
Xomw_MagicWord mw = new Xomw_MagicWord(name, cs, synonyms);
hash.Add(name, mw);
}
public Xomw_MagicWord Get(byte[] name) {
return null;
return (Xomw_MagicWord)hash.Get_by(name);
}
}

View File

@ -20,12 +20,24 @@ public class Xomw_MagicWordSynonym {
public final byte[] magic_name;
public final boolean case_match;
public final byte[] text;
public final byte[] text_wo_arg1;
public final byte arg1_tid;
public Xomw_MagicWordSynonym(byte[] magic_name, boolean case_match, byte[] text) {
this.magic_name = magic_name;
this.case_match = case_match;
this.text = text;
this.arg1_tid = Get_arg1_tid(text);
switch (arg1_tid) {
case Arg1__bgn:
text_wo_arg1 = Bry_.Mid(text, 2);
break;
case Arg1__end:
text_wo_arg1 = Bry_.Mid(text, 0, text.length - 2);
break;
default:
text_wo_arg1 = text;
break;
}
}
private static byte Get_arg1_tid(byte[] src) {
@ -53,11 +65,11 @@ public class Xomw_MagicWordSynonym {
else if (rv == Arg1__mid)
rv = Arg1__mix;
}
cur += 3;
cur += 2;
continue;
}
else {
cur += 2;
cur += 1;
continue;
}
}

View File

@ -487,7 +487,7 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
Xomw_image_params tmp_img_params = new Xomw_image_params();
this.getImageParams(tmp_img_params, handler2);
// Xomw_param_map paramMap = tmp_img_params.paramMap;
Xomw_param_map paramMap = tmp_img_params.paramMap;
Xomw_MagicWordArray mwArray = tmp_img_params.mwArray;
// XO.MW.UNSUPPORTED.TrackingCategory:
@ -498,8 +498,8 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// Process the input parameters
byte[] caption = Bry_.Empty;
// XO.MW: $params = [ 'frame' => [], 'handler' => [], 'horizAlign' => [], 'vertAlign' => [] ];
Xomw_prm_mgr param_map = new Xomw_prm_mgr();
Xomw_prm_mgr param_mgr = new Xomw_prm_mgr();
// Xomw_prm_mgr param_map = new Xomw_prm_mgr();
// Xomw_prm_mgr param_mgr = new Xomw_prm_mgr();
Xomw_img_prms frame = new Xomw_img_prms();
Xomw_mda_prms handler = new Xomw_mda_prms();
boolean seen_format = false;
@ -510,15 +510,14 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
byte[] part = parts[i];
part = Bry_.Trim(part);
byte[] magic_name = mwArray.matchVariableStartToEnd(part);
// byte[] magic_name = null;
boolean validated = false;
Xomw_prm_itm prm_itm = param_map.Get_or_null(magic_name);
Xomw_param_itm prm_itm = paramMap.Get_by(magic_name);
if (prm_itm != null) {
int prm_type = prm_itm.type;
int prm_name = prm_itm.name_type;
int prm_type = -1;
int paramNameType = prm_itm.name_type;
// Special case; width and height come in one variable together
if (prm_type == Xomw_prm_itm.Type__handler && prm_name == Xomw_prm_itm.Name__width) {
if (prm_type == Xomw_prm_itm.Type__handler && paramNameType == Xomw_prm_itm.Name__width) {
// $parsedWidthParam = $this->parseWidthParam($value);
// if (isset($parsedWidthParam['width'])) {
// $width = $parsedWidthParam['width'];
@ -543,17 +542,17 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
}
else {
// Validate @gplx.Internal protected parameters
switch (prm_name) {
case Xomw_prm_itm.Name__manual_thumb:
case Xomw_prm_itm.Name__alt:
case Xomw_prm_itm.Name__class:
switch (paramNameType) {
case Xomw_param_itm.Name__manual_thumb:
case Xomw_param_itm.Name__alt:
case Xomw_param_itm.Name__class:
// @todo FIXME: Possibly check validity here for
// manualthumb? downstream behavior seems odd with
// missing manual thumbs.
validated = true;
// $value = $this->stripAltText($value, $holders);
break;
case Xomw_prm_itm.Name__link:
case Xomw_param_itm.Name__link:
// $chars = self::EXT_LINK_URL_CLASS;
// $addr = self::EXT_LINK_ADDR;
// $prots = $this->mUrlProtocols;
@ -581,12 +580,13 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// }
// }
break;
case Xomw_prm_itm.Name__frameless:
case Xomw_prm_itm.Name__framed:
case Xomw_prm_itm.Name__thumbnail:
case Xomw_param_itm.Name__frameless:
case Xomw_param_itm.Name__framed:
case Xomw_param_itm.Name__thumbnail:
// use first appearing option, discard others.
validated = !seen_format;
seen_format = true;
frame.thumbnail = Bry_.Empty;
break;
default:
// Most other things appear to be empty or numeric...
@ -595,9 +595,9 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
}
}
}
// if ( $validated ) {
// $params[$type][$paramName] = $value;
// }
if (validated) {
// $params[$type][$paramName] = $value;
}
}
if (!validated) {
caption = part;
@ -605,13 +605,13 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
}
// Process alignment parameters
Xomw_prm_itm tmp = param_mgr.Get_or_null(Xomw_prm_mgr.Name__horiz_align);
Xomw_param_itm tmp = paramMap.Get_by(Xomw_prm_mgr.Name__horiz_align);
if (tmp != null) {
frame.align = tmp.val;
// frame.align = tmp.val;
}
tmp = param_mgr.Get_or_null(Xomw_prm_mgr.Name__vert_align);
tmp = paramMap.Get_by(Xomw_prm_mgr.Name__vert_align);
if (tmp != null) {
frame.valign = tmp.val;
// frame.valign = tmp.val;
}
frame.caption = caption;
@ -706,7 +706,7 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
internalParamNames = new Xomw_param_list[]
{ Xomw_param_list.New("horizAlign", "left", "right", "center", "none")
, Xomw_param_list.New("vertAlign", "baseline", "sub", "super", "top", "text-top", "middle", "bottom", "text-bottom")
, Xomw_param_list.New("thumbnail", "manual_thumb", "framed", "frameless", "upright", "border", "link", "alt", "class")
, Xomw_param_list.New("frame", "thumbnail", "manual_thumb", "framed", "frameless", "upright", "border", "link", "alt", "class")
};
internalParamMap = new Xomw_param_map();
@ -830,48 +830,3 @@ class Xomw_image_params {
public Xomw_param_map paramMap = null;
public Xomw_MagicWordArray mwArray = null;
}
class Xomw_param_map {
private final Ordered_hash hash = Ordered_hash_.New_bry();
public byte[][] Keys() {
int len = hash.Len();
byte[][] rv = new byte[len][];
for (int i = 0; i < len; i++) {
rv[i] = ((Xomw_param_itm)hash.Get_at(i)).magic;
}
return rv;
}
public void Add(byte[] magic, byte[] type, byte[] name) {
Xomw_param_itm itm = new Xomw_param_itm(magic, type, name);
hash.Add(magic, itm);
}
public Xomw_param_map Clone() {
Xomw_param_map rv = new Xomw_param_map();
int len = hash.Len();
for (int i = 0; i < len; i++) {
Xomw_param_itm itm = (Xomw_param_itm)hash.Get_at(i);
rv.Add(itm.magic, itm.type, itm.name);
}
return rv;
}
}
class Xomw_param_itm {
public final byte[] magic;
public final byte[] type;
public final byte[] name;
public Xomw_param_itm(byte[] magic, byte[] type, byte[] name) {
this.magic = magic;
this.type = type;
this.name = name;
}
}
class Xomw_param_list {
public byte[] type;
public byte[][] names;
public static Xomw_param_list New(String type, String... names) {
Xomw_param_list rv = new Xomw_param_list();
rv.type = Bry_.new_u8(type);
rv.names = Bry_.Ary(names);
return rv;
}
}

View File

@ -26,4 +26,7 @@ public class Xomw_lnki_wkr__file__tst {
@Test public void Plain() {
fxt.Test__to_html("[[File:A.png]]", "<img alt='A.png' src='/orig/7/70/A.png' />");
}
@Test public void Thumb() {
fxt.Test__to_html("[[File:A.png|thumb]]", "<div class='thumb t'><div class='thumbinner' style='width:2px;'> <div class='thumbcaption'></div></div></div><img alt='A.png' src='/orig/7/70/A.png' />");
}
}

View File

@ -41,7 +41,8 @@ class Xomw_lnki_wkr__fxt {
wkr = parser.Lnki_wkr();
// env
parser.Env().File_finder_(file_finder);
parser.Env().File_finder_(file_finder);
parser.Env().Magic_word_mgr().Add(Bry_.new_u8("img_thumbnail"), Bool_.Y, Bry_.Ary("thumb"));
parser.Init_by_wiki(wiki);
// ctx

View File

@ -0,0 +1,42 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_param_itm {
public final byte[] magic;
public final byte[] type;
public final byte[] name;
public final int name_type;
public Xomw_param_itm(byte[] magic, byte[] type, byte[] name) {
this.magic = magic;
this.type = type;
this.name = name;
this.name_type = name_types.Get_as_int_or(name, -1);
}
public static final int
Name__width = 0
, Name__manual_thumb = 1
, Name__alt = 2
, Name__class = 3
, Name__link = 4
, Name__frameless = 5
, Name__framed = 6
, Name__thumbnail = 7
;
private static final Hash_adp_bry name_types = Hash_adp_bry.cs()
.Add_str_int("thumbnail", Name__thumbnail);
}

View File

@ -0,0 +1,59 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_param_map {
private final Ordered_hash hash = Ordered_hash_.New_bry();
public Xomw_param_itm Get_by(byte[] name) {
return (Xomw_param_itm)hash.Get_by(name);
}
public Xomw_param_itm Get_by(int name_type) {
return null;
}
public byte[][] Keys() {
int len = hash.Len();
byte[][] rv = new byte[len][];
for (int i = 0; i < len; i++) {
rv[i] = ((Xomw_param_itm)hash.Get_at(i)).magic;
}
return rv;
}
public void Add(byte[] magic, byte[] type, byte[] name) {
Xomw_param_itm itm = new Xomw_param_itm(magic, type, name);
hash.Add(magic, itm);
}
public Xomw_param_map Clone() {
Xomw_param_map rv = new Xomw_param_map();
int len = hash.Len();
for (int i = 0; i < len; i++) {
Xomw_param_itm itm = (Xomw_param_itm)hash.Get_at(i);
rv.Add(itm.magic, itm.type, itm.name);
}
return rv;
}
}
class Xomw_param_list {
public byte[] type;
public byte[][] names;
public static Xomw_param_list New(String type, String... names) {
Xomw_param_list rv = new Xomw_param_list();
rv.type = Bry_.new_u8(type);
rv.names = Bry_.Ary(names);
return rv;
}
}