1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.core.btries.*; import gplx.intl.*;
import gplx.xowa.langs.cnvs.*;
public class Xol_vnt_converter {
private Btrie_slim_mgr trie = Btrie_slim_mgr.cs_();
public Xol_vnt_converter(Xol_vnt_itm owner) {this.owner = owner;}
public byte[] Owner_key() {return owner.Key();}
public Xol_vnt_itm Owner() {return owner;} private Xol_vnt_itm owner;
public boolean Convert_text(Bry_bfr bfr, byte[] src) {return Convert_text(bfr, src, 0, src.length);}
public boolean Convert_text(Bry_bfr bfr, byte[] src, int bgn, int end) {
int pos = bgn;
boolean matched = false;
while (pos < end) {
byte b = src[pos];
Object o = trie.Match_bgn_w_byte(b, src, pos, end);
if (o == null) { // no match; skip to next char
int char_len = Utf8_.Len_of_char_by_1st_byte(b); // NOTE: must increment by char_len, not +1
if (matched) {
if (char_len == 1)
bfr.Add_byte(b);
else
bfr.Add_mid(src, pos, pos + char_len);
}
pos += char_len;
}
else {
if (!matched) {
bfr.Add_mid(src, bgn, pos); // add everything up to pos
matched = true;
}
bfr.Add((byte[])o);
pos = trie.Match_pos();
}
}
return matched;
}
public void Rebuild() {Clear(); Build();}
private void Clear() {trie.Clear();}
private void Build() {
Xol_lang lang = owner.Lang();
byte[][] convert_ary = owner.Convert_ary();
int convert_ary_len = convert_ary.length;
for (int i = 0; i < convert_ary_len; i++) {
byte[] convert_grp_key = convert_ary[i];
Xol_cnv_grp convert_grp = lang.Cnv_mgr().Get_or_null(convert_grp_key);
if (convert_grp == null) continue; // vnts may not have convert mapping; EX: zh-my
Build_grp(convert_grp);
}
}
private void Build_grp(Xol_cnv_grp convert_grp) {
int len = convert_grp.Len();
for (int i = 0; i < len; i++) {
Xol_cnv_itm convert_itm = convert_grp.Get_at(i);
trie.Add_obj(convert_itm.Src(), convert_itm.Trg()); // NOTE: for dupes, latest value wins
}
}
}

View File

@@ -0,0 +1,40 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.intl.*;
import gplx.xowa.langs.cnvs.*;
public class Xol_vnt_itm implements GfoInvkAble {
public Xol_vnt_itm(Xol_vnt_mgr owner, byte[] key) {
this.owner = owner;
this.lang = owner.Lang(); this.key = key;
converter = new Xol_vnt_converter(this);
}
public Xol_vnt_mgr Owner() {return owner;} private Xol_vnt_mgr owner;
public Xol_lang Lang() {return lang;} private Xol_lang lang;
public Xol_vnt_converter Converter() {return converter;} private Xol_vnt_converter converter;
public byte[] Key() {return key;} private byte[] key;
public byte[][] Fallback_ary() {return fallback_ary;} private byte[][] fallback_ary = Bry_.Ary_empty;
public byte[][] Convert_ary() {return convert_ary;} private byte[][] convert_ary = Bry_.Ary_empty;
public void Convert_ary_(byte[][] v) {convert_ary = v;}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_fallbacks_)) fallback_ary = Bry_.Split(m.ReadBry("v"), Byte_ascii.Pipe);
else if (ctx.Match(k, Invk_converts_)) {convert_ary = Bry_.Split(m.ReadBry("v"), Byte_ascii.Pipe); converter.Rebuild();} // setting converts will always force rebuild
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_fallbacks_ = "fallbacks_", Invk_converts_ = "converts_";
}

View File

@@ -0,0 +1,105 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.xowa.wikis.data.tbls.*;
public class Xol_vnt_mgr implements GfoInvkAble {
private Ordered_hash vnts = Ordered_hash_.new_bry_();
private int converter_ary_len; private Ordered_hash tmp_page_list = Ordered_hash_.new_bry_();
public Xol_vnt_mgr(Xol_lang lang) {this.lang = lang;}
public Xol_vnt_converter[] Converter_ary() {return converter_ary;} private Xol_vnt_converter[] converter_ary;
public Xolg_vnt_grp Vnt_grp() {return vnt_grp;} private Xolg_vnt_grp vnt_grp = new Xolg_vnt_grp();
public Xolg_vnt_grp_fmtr Vnt_mnu_fmtr() {return vnt_mnu_fmtr;} private Xolg_vnt_grp_fmtr vnt_mnu_fmtr = new Xolg_vnt_grp_fmtr();
public Xol_lang Lang() {return lang;} private Xol_lang lang;
public byte[] Cur_vnt() {return cur_vnt;} public Xol_vnt_mgr Cur_vnt_(byte[] v) {cur_vnt = v; return this;} private byte[] cur_vnt = Bry_.Empty;
public boolean Enabled() {return enabled;} public void Enabled_(boolean v) {this.enabled = v;} private boolean enabled = false;
public String Html_style() {return html_style;} private String html_style = "";
public void Init_by_wiki(Xowe_wiki wiki) {
if (!enabled) return;
Xop_vnt_lxr_.set_(wiki);
// VNT
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-hans"), Bry_.new_u8("Simplified")));
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-hant"), Bry_.new_u8("Traditional")));
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-cn"), Bry_.new_u8("China")));
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-hk"), Bry_.new_u8("Hong Kong")));
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-mo"), Bry_.new_u8("Macau")));
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-sg"), Bry_.new_u8("Singapore")));
// vnt_grp.Add(new Xolg_vnt_itm(Bry_.new_u8("zh-tw"), Bry_.new_u8("Taiwan")));
}
public Xol_vnt_itm Get_or_new(byte[] key) {
Xol_vnt_itm rv = (Xol_vnt_itm)vnts.Get_by(key);
if (rv == null) {
rv = new Xol_vnt_itm(this, key);
vnts.Add(key, rv);
enabled = true; // mark enabled if any vnts have been added
}
return rv;
}
public void Convert_ttl_init() {
int vnts_len = vnts.Count();
converter_ary_len = vnts_len;
converter_ary = new Xol_vnt_converter[vnts_len];
for (int i = 0; i < vnts_len; i++) {
Xol_vnt_itm itm = (Xol_vnt_itm)vnts.Get_at(i);
converter_ary[i] = itm.Converter();
if (i == 0) cur_vnt = itm.Key(); // default to 1st item
}
}
public Xowd_page_itm Convert_ttl(Xowe_wiki wiki, Xoa_ttl ttl) {return Convert_ttl(wiki, ttl.Ns(), ttl.Page_db());} // NOTE: not Full_db as ttl.Ns is passed; EX:Шаблон:Šablon:Jez-eng; PAGE:sr.w:ДНК DATE:2014-07-06
public Xowd_page_itm Convert_ttl(Xowe_wiki wiki, Xow_ns ns, byte[] ttl_bry) {
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_b512();
Xowd_page_itm rv = Convert_ttl(wiki, tmp_bfr, ns, ttl_bry);
tmp_bfr.Mkr_rls();
return rv;
}
public Xowd_page_itm Convert_ttl(Xowe_wiki wiki, Bry_bfr tmp_bfr, Xow_ns ns, byte[] ttl_bry) { // REF.MW:LanguageConverter.php|findVariantLink
int converted = Convert_ttl_convert(wiki, tmp_bfr, ns, ttl_bry); // convert ttl for each vnt
if (converted == 0) return Xowd_page_itm.Null; // ttl_bry has no conversions; exit;
wiki.Db_mgr().Load_mgr().Load_by_ttls(Cancelable_.Never, tmp_page_list, true, 0, converted);
for (int i = 0; i < converted; i++) {
Xowd_page_itm page = (Xowd_page_itm)tmp_page_list.Get_at(i);
if (page.Exists()) return page; // return 1st found page
}
return Xowd_page_itm.Null;
}
private int Convert_ttl_convert(Xowe_wiki wiki, Bry_bfr tmp_bfr, Xow_ns ns, byte[] ttl_bry) {
tmp_page_list.Clear();
int rv = 0;
for (int i = 0; i < converter_ary_len; i++) { // convert ttl for each variant
Xol_vnt_converter converter = converter_ary[i];
tmp_bfr.Clear();
if (!converter.Convert_text(tmp_bfr, ttl_bry)) continue; // ttl is not converted for variant; ignore
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ns.Id(), tmp_bfr.Xto_bry_and_clear()); // NOTE: must convert to ttl in order to upper 1st letter; EX:{{jez-eng|sense}} -> Jez-eng; PAGE:sr.w:ДНК DATE:2014-07-06
if (ttl == null) continue;
Xowd_page_itm page = new Xowd_page_itm();
page.Ttl_(ns, ttl.Page_db());
byte[] converted_ttl = page.Ttl_full_db();
if (tmp_page_list.Has(converted_ttl)) continue;
tmp_page_list.Add(converted_ttl, page);
++rv;
}
return rv;
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_get)) return Get_or_new(m.ReadBry("v"));
else if (ctx.Match(k, Invk_init_end)) Convert_ttl_init();
else if (ctx.Match(k, Invk_cur_vnt_)) cur_vnt = m.ReadBry("v");
else if (ctx.Match(k, Invk_html_style_)) html_style = m.ReadStr("v");
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_get = "get", Invk_init_end = "init_end", Invk_cur_vnt_ = "cur_vnt_", Invk_html_style_ = "html_style_";
}

View File

@@ -0,0 +1,25 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
public class Xolg_vnt_grp {
private final List_adp list = List_adp_.new_();
public byte[] Text() {return text;} public void Text_(byte[] v) {text = v;} private byte[] text;
public int Len() {return list.Count();}
public Xolg_vnt_itm Get_at(int i) {return (Xolg_vnt_itm)list.Get_at(i);}
public void Add(Xolg_vnt_itm itm) {list.Add(itm);}
}

View File

@@ -0,0 +1,58 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
public class Xolg_vnt_grp_fmtr implements Bry_fmtr_arg {
private Xolg_vnt_grp grp;
private Xolg_vnt_itm_fmtr itm_fmtr = new Xolg_vnt_itm_fmtr();
public void Init(Xolg_vnt_grp grp, byte[] page_href, byte[] page_vnt) {
this.grp = grp;
itm_fmtr.Init(grp, page_href, page_vnt);
}
public void XferAry(Bry_bfr bfr, int idx) {
fmtr.Bld_bfr_many(bfr, grp.Text(), itm_fmtr);
}
private static final Bry_fmtr fmtr = Bry_fmtr.new_(String_.Concat_lines_nl_skip_last
( " <div id='p-variants' role='navigation' class='vectorMenu' aria-labelledby='p-variants-label'>"
, " <h3 id='p-variants-label'><span>~{grp_text}</span><a href='#'></a></h3>"
, " <div class='menu'>"
, " <ul>~{itms}"
, " </ul>"
, " </div>"
, " </div>"
), "grp_text", "itms"
);
}
class Xolg_vnt_itm_fmtr implements Bry_fmtr_arg {
private Xolg_vnt_grp grp; private byte[] page_href, page_vnt;
public void Init(Xolg_vnt_grp grp, byte[] page_href, byte[] page_vnt) {this.grp = grp; this.page_href = page_href; this.page_vnt = page_vnt;}
public void XferAry(Bry_bfr bfr, int idx) {
int len = grp.Len();
for (int i = 0; i < len; ++i) {
Xolg_vnt_itm itm = grp.Get_at(i);
boolean itm_is_selected = Bry_.Eq(itm.Key(), page_vnt);
byte[] itm_cls_selected = itm_is_selected ? Itm_cls_selected_y : Bry_.Empty;
fmtr.Bld_bfr_many(bfr, i, itm_cls_selected, itm.Key(), itm.Text(), page_href);
}
}
private static final byte[] Itm_cls_selected_y = Bry_.new_a7(" class='selected'");
private static final Bry_fmtr fmtr = Bry_fmtr.new_(String_.Concat_lines_nl_skip_last
( ""
, " <li id='ca-varlang-~{itm_idx}'~{itm_cls_selected}><a href='/wiki/~{itm_href}?xowa_vnt=~{itm_lang}' lang='~{itm_lang}' hreflang='~{itm_lang}'>~{itm_text}</a></li>"
), "itm_idx", "itm_cls_selected", "itm_lang", "itm_text", "itm_href"
);
}

View File

@@ -0,0 +1,68 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import org.junit.*;
public class Xolg_vnt_grp_fmtr_tst {
@Before public void init() {fxt.Clear();} private final Xolg_vnt_grp_fmtr_fxt fxt = new Xolg_vnt_grp_fmtr_fxt();
@Test public void Basic() {
fxt.Test_to_str("Earth", "zh-hk", String_.Concat_lines_nl_skip_last
( " <div id='p-variants' role='navigation' class='vectorMenu' aria-labelledby='p-variants-label'>"
, " <h3 id='p-variants-label'><span>Choose lang</span><a href='#'></a></h3>"
, " <div class='menu'>"
, " <ul>"
, " <li id='ca-varlang-0'><a href='/wiki/Earth?xowa_vnt=zh-hans' lang='zh-hans' hreflang='zh-hans'>Simplified</a></li>"
, " <li id='ca-varlang-1'><a href='/wiki/Earth?xowa_vnt=zh-hant' lang='zh-hant' hreflang='zh-hant'>Traditional</a></li>"
, " <li id='ca-varlang-2'><a href='/wiki/Earth?xowa_vnt=zh-cn' lang='zh-cn' hreflang='zh-cn'>China</a></li>"
, " <li id='ca-varlang-3' class='selected'><a href='/wiki/Earth?xowa_vnt=zh-hk' lang='zh-hk' hreflang='zh-hk'>Hong Kong</a></li>"
, " <li id='ca-varlang-4'><a href='/wiki/Earth?xowa_vnt=zh-mo' lang='zh-mo' hreflang='zh-mo'>Macau</a></li>"
, " <li id='ca-varlang-5'><a href='/wiki/Earth?xowa_vnt=zh-sg' lang='zh-sg' hreflang='zh-sg'>Singapore</a></li>"
, " <li id='ca-varlang-6'><a href='/wiki/Earth?xowa_vnt=zh-tw' lang='zh-tw' hreflang='zh-tw'>Taiwan</a></li>"
, " </ul>"
, " </div>"
, " </div>"
));
}
}
class Xolg_vnt_grp_fmtr_fxt {
private Xolg_vnt_grp vnt_grp;
public void Clear() {
this.Init_grp("Choose lang", "zh-hans", "Simplified", "zh-hant", "Traditional", "zh-cn", "China", "zh-hk", "Hong Kong", "zh-mo", "Macau", "zh-sg", "Singapore", "zh-tw", "Taiwan");
}
public void Init_grp(String text, String... langs) {
vnt_grp = new Xolg_vnt_grp();
vnt_grp.Text_(Bry_.new_u8(text));
int len = langs.length;
String lang_code = "";
for (int i = 0; i < len; ++i) {
String lang = langs[i];
if (i % 2 == 0)
lang_code = lang;
else {
Xolg_vnt_itm itm = new Xolg_vnt_itm(Bry_.new_u8(lang_code), Bry_.new_u8(lang));
vnt_grp.Add(itm);
}
}
}
public void Test_to_str(String page_href, String selected_vnt, String expd) {
Xolg_vnt_grp_fmtr vnt_grp_fmtr = new Xolg_vnt_grp_fmtr();
Bry_bfr bfr = Bry_bfr.new_();
vnt_grp_fmtr.Init(vnt_grp, Bry_.new_u8(page_href), Bry_.new_u8(selected_vnt));
vnt_grp_fmtr.XferAry(bfr, 0);
Tfds.Eq_str_lines(expd, bfr.Xto_str_and_clear());
}
}

View File

@@ -0,0 +1,23 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
public class Xolg_vnt_itm {
public Xolg_vnt_itm(byte[] key, byte[] text) {this.key = key; this.text = text;}
public byte[] Key() {return key;} private final byte[] key;
public byte[] Text() {return text;} private final byte[] text;
}

View File

@@ -0,0 +1,22 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
public class Xop_vnt_eqgt_tkn extends Xop_tkn_itm_base {
public Xop_vnt_eqgt_tkn(int bgn, int end) {this.Tkn_ini_pos(false, bgn, end);}
@Override public byte Tkn_tid() {return Xop_tkn_itm_.Tid_vnt_eqgt;}
}

View File

@@ -0,0 +1,124 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.core.btries.*;
public class Xop_vnt_flag {
public Xop_vnt_flag(byte tid) {this.tid = tid; this.langs = Bry_.Ary_empty;}
public Xop_vnt_flag(byte tid, byte[][] langs) {this.tid = tid; this.langs = langs;}
public byte Tid() {return tid;} private byte tid;
public byte[][] Langs() {return langs;} private byte[][] langs;
}
class Xop_vnt_flag_ {
public static final Xop_vnt_flag[] Ary_empty = new Xop_vnt_flag[0];
public static final byte
Tid_unknown = 0
, Tid_show = 1 // EX: -{S|zh-hans:A;zh-hant:B}- -> "A"
, Tid_all = 2 // EX: -{+|zh-hans:A;zh-hant:B}- -> "A"
, Tid_err = 3 // EX: -{E|zh-hans:A;zh-hant:B}- -> "A"
, Tid_add = 4 // add and output; EX: -{A|zh-hans:A;zh-hant:B}- -> "A"
, Tid_title = 5 // page_title; EX: -{T|zh-hans:A;zh-hant:B}- -> ""
, Tid_raw = 6 // raw: no convert; EX: -{R|zh-hans:A;zh-hant:B}- -> "zh-hans:A;zh-hant:B"
, Tid_descrip = 7 // describe; EX: -{D|zh-hans:A;zh-hant:B}- -> "简体A繁體B" (简体=Simplified;繁體=Traditional)
, Tid_del = 8 // remove; EX: -{-|zh-hans:A;zh-hant:B}- -> ""
, Tid_macro = 9 // macro; EX: -{H|zh-hans:A;zh-hant:B}- -> ""
, Tid_name = 10 // EX: -{N|zh-hans:A;zh-hant:B}- -> ""
, Tid_lang = 11 // EX: -{zh-hant|B}- -> "B"
;
public static String Xto_name(byte tid) {
switch (tid) {
case Tid_unknown: return "unknown";
case Tid_show : return "show";
case Tid_all : return "all";
case Tid_err : return "err";
case Tid_add : return "add";
case Tid_title : return "title";
case Tid_raw : return "raw";
case Tid_descrip: return "descrip";
case Tid_del : return "del";
case Tid_macro : return "macro";
case Tid_name : return "name";
case Tid_lang : return "lang";
default : throw Exc_.new_unhandled(tid);
}
}
public static final byte Tid__max = 12;
public static final byte
Key_show = Byte_ascii.Ltr_S
, Key_all = Byte_ascii.Plus
, Key_err = Byte_ascii.Ltr_E
, Key_add = Byte_ascii.Ltr_A
, Key_title = Byte_ascii.Ltr_T
, Key_raw = Byte_ascii.Ltr_R
, Key_descrip = Byte_ascii.Ltr_D
, Key_del = Byte_ascii.Dash
, Key_macro = Byte_ascii.Ltr_H
, Key_name = Byte_ascii.Ltr_N
;
public static final Xop_vnt_flag
Flag_unknown = new Xop_vnt_flag(Tid_unknown)
, Flag_show = new Xop_vnt_flag(Tid_show)
, Flag_all = new Xop_vnt_flag(Tid_all)
, Flag_err = new Xop_vnt_flag(Tid_err)
, Flag_add = new Xop_vnt_flag(Tid_add)
, Flag_title = new Xop_vnt_flag(Tid_title)
, Flag_raw = new Xop_vnt_flag(Tid_raw)
, Flag_descrip = new Xop_vnt_flag(Tid_descrip)
, Flag_del = new Xop_vnt_flag(Tid_del)
, Flag_macro = new Xop_vnt_flag(Tid_macro)
, Flag_name = new Xop_vnt_flag(Tid_name)
;
public static final Btrie_fast_mgr Trie = Btrie_fast_mgr.ci_ascii_() // NOTE: match either lc or uc; EX: -{D}- or -{d}-; // NOTE:ci.ascii:MW_const.en; flag keys; EX: -{S|a}-
.Add(Byte_ascii.Ltr_S , Xop_vnt_flag_.Flag_show)
.Add(Byte_ascii.Plus , Xop_vnt_flag_.Flag_all)
.Add(Byte_ascii.Ltr_E , Xop_vnt_flag_.Flag_err)
.Add(Byte_ascii.Ltr_A , Xop_vnt_flag_.Flag_add)
.Add(Byte_ascii.Ltr_T , Xop_vnt_flag_.Flag_title)
.Add(Byte_ascii.Ltr_R , Xop_vnt_flag_.Flag_raw)
.Add(Byte_ascii.Ltr_D , Xop_vnt_flag_.Flag_descrip)
.Add(Byte_ascii.Dash , Xop_vnt_flag_.Flag_del)
.Add(Byte_ascii.Ltr_H , Xop_vnt_flag_.Flag_macro)
.Add(Byte_ascii.Ltr_N , Xop_vnt_flag_.Flag_name)
;
public static Xop_vnt_flag new_langs_(byte[][] langs) {return new Xop_vnt_flag(Tid_lang, langs);}
}
class Xop_vnt_flag_ary_bldr {
private Xop_vnt_flag[] ary = new Xop_vnt_flag[Xop_vnt_flag_.Tid__max];
private int add_count = 0;
public void Clear() {
for (int i = 0; i < Xop_vnt_flag_.Tid__max; i++)
ary[i] = null;
add_count = 0;
}
public void Add(Xop_vnt_flag flag) {
int idx = flag.Tid();
if (ary[idx] == null) {
ary[idx] = flag;
++add_count;
}
}
public Xop_vnt_flag[] Bld() {
Xop_vnt_flag[] rv = new Xop_vnt_flag[add_count];
int rv_idx = 0;
for (int i = 0; i < Xop_vnt_flag_.Tid__max; i++) {
Xop_vnt_flag itm = ary[i];
if (itm != null)
rv[rv_idx++] = itm;
}
return rv;
}
}

View File

@@ -0,0 +1,64 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.core.btries.*;
class Xop_vnt_flag_lang_bldr {
private Xop_vnt_flag_lang_itm[] ary; private int ary_len;
private int ary_count;
public Xop_vnt_flag_lang_bldr(Xol_vnt_mgr vnt_mgr) {
Xol_vnt_converter[] converter_ary = vnt_mgr.Converter_ary();
int len = converter_ary.length;
for (int i = 0; i < len; i++) {
byte[] lang = converter_ary[i].Owner().Key();
Xop_vnt_flag_lang_itm itm = new Xop_vnt_flag_lang_itm(i, lang);
trie.Add_obj(lang, itm);
}
ary = new Xop_vnt_flag_lang_itm[len];
ary_len = len;
}
public Btrie_slim_mgr Trie() {return trie;} private Btrie_slim_mgr trie = Btrie_slim_mgr.ci_ascii_(); // NOTE:ci.ascii:MW_const.en; lang variant name; EX:zh-hans
public void Add(Xop_vnt_flag_lang_itm itm) {
int idx = itm.Idx();
if (ary[idx] == null) {
ary[idx] = itm;
++ary_count;
}
}
public void Clear() {
for (int i = 0; i < ary_len; i++)
ary[i] = null;
ary_count = 0;
}
public Xop_vnt_flag Bld() {
if (ary_count == 0) return Xop_vnt_flag_.Flag_unknown;
byte[][] langs = new byte[ary_count][];
int ary_idx = 0;
for (int i = 0; i < ary_len; i++) {
Xop_vnt_flag_lang_itm itm = ary[i];
if (itm != null)
langs[ary_idx++] = itm.Key();
}
return Xop_vnt_flag_.new_langs_(langs);
}
}
class Xop_vnt_flag_lang_itm {
public Xop_vnt_flag_lang_itm(int idx, byte[] key) {this.idx = idx; this.key = key; this.key_len = key.length;}
public int Idx() {return idx;} private int idx;
public byte[] Key() {return key;} private byte[] key;
public int Key_len() {return key_len;} private int key_len;
}

View File

@@ -0,0 +1,111 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.core.btries.*;
class Xop_vnt_flag_parser {
private Xop_vnt_flag_lang_bldr flag_lang_bldr;
public Xop_vnt_flag_parser(Xol_vnt_mgr vnt_mgr) {flag_lang_bldr = new Xop_vnt_flag_lang_bldr(vnt_mgr);}
private void Clear() {
flag_lang_bldr.Clear();
}
public int Rslt_tkn_pos() {return rslt_tkn_pos;} private int rslt_tkn_pos;
public int Rslt_pipe_last() {return rslt_pipe_last;} private int rslt_pipe_last;
public Xop_vnt_flag[] Rslt_flags() {return rslt_flags;} private Xop_vnt_flag[] rslt_flags;
public void Parse(Xowe_wiki wiki, Xop_vnt_tkn vnt_tkn, int pipe_tkn_count, byte[] src) {
this.Clear();
rslt_flags = new Xop_vnt_flag[pipe_tkn_count];
int rv_idx = 0;
int subs_len = vnt_tkn.Subs_len();
Bry_bfr flag_bfr = wiki.Utl__bfr_mkr().Get_b128();
rslt_tkn_pos = 0;
boolean loop = true;
while (true) {
Xop_tkn_itm sub = vnt_tkn.Subs_get(rslt_tkn_pos);
switch (sub.Tkn_tid()) {
case Xop_tkn_itm_.Tid_txt:
flag_bfr.Add_mid(src, sub.Src_bgn(), sub.Src_end());
break;
case Xop_tkn_itm_.Tid_pipe:
rslt_flags[rv_idx++] = Parse_flag_bry(flag_bfr.Xto_bry_and_clear());
if (rv_idx == pipe_tkn_count) {
loop = false;
rslt_pipe_last = sub.Src_end();
}
break;
case Xop_tkn_itm_.Tid_space:
case Xop_tkn_itm_.Tid_tab:
case Xop_tkn_itm_.Tid_newLine: // skip ws
break;
default:
wiki.Appe().Usr_dlg().Log_many("", "", "unknown tkn in vnt flag; tid=~{0} txt=~{1}", sub.Tkn_tid(), String_.new_u8(src, sub.Src_bgn(), sub.Src_end()));
flag_bfr.Add_mid(src, sub.Src_bgn(), sub.Src_end());
break;
}
++rslt_tkn_pos;
if (rslt_tkn_pos == subs_len) break;
if (!loop) break;
}
flag_bfr.Mkr_rls();
}
private Xop_vnt_flag Parse_flag_bry(byte[] bry) {
int bry_len = bry.length;
if (bry_len == 0) return Xop_vnt_flag_.Flag_unknown; // EX: exit early if 0 len, else trie will fail; EX: "-{|}-"
Object flag_obj = flag_trie.Match_exact(bry, 0, bry_len);
return flag_obj == null
? Parse_flag_vnts(bry, bry_len) // unknown tid sequence; either (a) "lang" cmd ("-{zh-hans;zh-hant|a}-") or (b) invalid cmd ("-{X|a}-")
: (Xop_vnt_flag)flag_obj; // known flag; check that next non_ws is |
}
private Xop_vnt_flag Parse_flag_vnts(byte[] bry, int bry_len) {
boolean loop = true;
int vnt_pos = 0;
Btrie_slim_mgr trie = flag_lang_bldr.Trie();
while (loop) {
boolean last = false;
boolean valid = true;
Object vnt_obj = trie.Match_bgn(bry, vnt_pos, bry_len);
if (vnt_obj == null) break; // no more vnts found; stop
vnt_pos = trie.Match_pos(); // update pos to end of vnt
int semic_pos = Bry_finder.Find_fwd_while_not_ws(bry, vnt_pos, bry_len);
if (semic_pos == bry_len) // note that Find_fwd_non_ws will return bry_len if no non-ws found;
last = true;
else { // char found; make sure it is semic
if (bry[semic_pos] != Byte_ascii.Semic) { // invalid vnt; ignore; EX: -{zh-hansx|}-
valid = false;
}
vnt_pos = semic_pos + 1; // update pos to after semic
if (vnt_pos == bry_len) last = true; // EX: "a;"
}
if (valid)
flag_lang_bldr.Add((Xop_vnt_flag_lang_itm)vnt_obj);
else // invalid entry clears list; EX: -{zh-hans;zh-bad}-
flag_lang_bldr.Clear();
if (last) break;
}
return flag_lang_bldr.Bld();
}
private static Btrie_fast_mgr flag_trie = Xop_vnt_flag_.Trie;
// private static final byte Dlm_tid_bgn = 0, Dlm_tid_end = 1, Dlm_tid_pipe = 2, Dlm_tid_colon = 3, Dlm_tid_semic = 4, Dlm_tid_kv = 5;
// private static Btrie_fast_mgr dlm_trie = Btrie_fast_mgr.cs_()
// .Add_bry_bval(Xop_vnt_lxr_.Hook_bgn , Dlm_tid_bgn)
// .Add_bry_bval(Xop_vnt_lxr_.Hook_end , Dlm_tid_end)
// .Add_bry_bval(Byte_ascii.Pipe , Dlm_tid_pipe)
// .Add_bry_bval(Byte_ascii.Colon , Dlm_tid_colon)
// .Add_bry_bval(Byte_ascii.Semic , Dlm_tid_semic)
// .Add_bry_bval(Bry_.new_a7("=>") , Dlm_tid_kv)
// ;
}

View File

@@ -0,0 +1,82 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.xowa.html.*;
public class Xop_vnt_html_wtr {
public static void Write(Bry_bfr bfr, Xoh_html_wtr html_wtr, Xop_ctx ctx, Xoh_wtr_ctx hctx, Xoae_page page, byte[] src, Xop_vnt_tkn vnt) {
byte[] cur_lang_vnt = ctx.Wiki().Lang().Vnt_mgr().Cur_vnt();
Xop_vnt_rule[] rules = vnt.Vnt_rules(); if (rules == null) return; // shouldn't happen, but guard anyway
int rules_len = rules.length;
switch (vnt.Vnt_cmd()) {
case Xop_vnt_html_wtr.Cmd_empty: break; // nothing: ""
case Xop_vnt_html_wtr.Cmd_error: // original token; "-{A}-"
bfr.Add_mid(src, vnt.Src_bgn(), vnt.Src_end());
break;
case Xop_vnt_html_wtr.Cmd_literal: { // val only; "A"
Xop_vnt_rule rule_0 = rules[0]; // Cmd_calc guarantees there will always be 1 item
html_wtr.Write_tkn_ary(bfr, ctx, hctx, src, rule_0.Rule_subs());
break;
}
case Xop_vnt_html_wtr.Cmd_bidi: { // matching rule: "A" if zh-hans; -{zh-hans:A}-
Xop_vnt_rule rule = Get_rule_by_key(rules, rules_len, cur_lang_vnt);
if (rule != null) html_wtr.Write_tkn_ary(bfr, ctx, hctx, src, rule.Rule_subs());
break;
}
case Xop_vnt_html_wtr.Cmd_lang: { // matching lang: "A" if zh-hans; -{zh-hans|A}-
Xop_vnt_rule rule_0 = rules[0]; // Cmd_calc guarantees there will always be 1 rule
Xop_vnt_flag flag_0 = vnt.Vnt_flags()[0]; // parse guarantees there will always be 1 flag
byte[][] langs = flag_0.Langs();
int flags_len = langs.length;
for (int i = 0; i < flags_len; i++) {
byte[] lang = langs[i];
if (Bry_.Eq(lang, cur_lang_vnt)) {
html_wtr.Write_tkn_ary(bfr, ctx, hctx, src, rule_0.Rule_subs());
break;
}
}
break;
}
case Xop_vnt_html_wtr.Cmd_raw: { // raw; everything between last flag and }-: "-{R|zh-hans:A;zh-hant:B}- -> "zh-hans:A;zh-hant:B"
bfr.Add_mid(src, vnt.Vnt_pipe_idx_last(), vnt.Src_end() - 2);
break;
}
case Xop_vnt_html_wtr.Cmd_descrip: { // descrip; similar to raw, but use localized lang
// bfr.Add_mid(src, vnt.Vnt_pipe_idx_last(), vnt.Src_end() - 2);
break;
}
case Xop_vnt_html_wtr.Cmd_title: break; // title: ignore; already handled during parse; DATE:2014-08-29
}
}
public static Xop_vnt_rule Get_rule_by_key(Xop_vnt_rule[] rules, int rules_len, byte[] cur_lang_vnt) {
for (int i = 0; i < rules_len; i++) {
Xop_vnt_rule rule = rules[i];
if (Bry_.Eq(rule.Rule_lang(), cur_lang_vnt)) return rule;
}
return null;
}
public static final byte
Cmd_error = 0 // eror -> output literal; EX: "-{some_unknown_error}-" -> "-{some_unknown_error}-"
, Cmd_empty = 1 // empty -> output nothing; EX: "-{}-" -> ""
, Cmd_literal = 2 // literal EX: "-{A}-" -> "A"
, Cmd_bidi = 3 // bidi EX: "-{zh-hans:A;zh-hant:B}-" -> "A" if zh-hans; "B" if zh-hant
, Cmd_lang = 4 // lang EX: "-{zh-hans|A}-" -> "A" if zh-hans; "" if zh-hant
, Cmd_raw = 5 // raw; text in -{}- EX: "-{R|zh-hans:A;zh-hant:B}- -> "zh-hans:A;zh-hant:B"
, Cmd_descrip = 6 // describe; output rules EX: "-{D|zh-hans:A;zh-hant:B}- -> "简体A繁體B"
, Cmd_title = 7 // title; change title EX: "-{T|zh-hans:A;zh-hant:B}- -> "A" as display title
;
}

View File

@@ -0,0 +1,95 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.core.btries.*;
public class Xop_vnt_lxr_ {
public static void set_(Xowe_wiki wiki) {
Btrie_fast_mgr wiki_trie = wiki.Parser().Wtxt_trie();
Object exists = wiki_trie.Match_bgn(Xop_vnt_lxr_.Hook_bgn, 0, Xop_vnt_lxr_.Hook_bgn.length);
if (exists == null) {
Xop_vnt_lxr_eqgt._.Init_by_wiki(wiki, wiki_trie);
Xop_vnt_lxr_bgn._.Init_by_wiki(wiki, wiki_trie);
new Xop_vnt_lxr_end().Init_by_wiki(wiki, wiki_trie);
// Btrie_fast_mgr tmpl_trie = wiki.Parser().Tmpl_trie(); // do not add to tmpl trie
// Xop_vnt_lxr_bgn._.Init_by_wiki(wiki, tmpl_trie);
}
}
public static final byte[] Hook_bgn = new byte[] {Byte_ascii.Dash, Byte_ascii.Curly_bgn}, Hook_end = new byte[] {Byte_ascii.Curly_end, Byte_ascii.Dash};
}
class Xop_vnt_lxr_eqgt implements Xop_lxr {
public byte Lxr_tid() {return Xop_lxr_.Tid_vnt_eqgt;}
public void Init_by_wiki(Xowe_wiki wiki, Btrie_fast_mgr core_trie) {core_trie.Add(Hook, this);}
public void Init_by_lang(Xol_lang lang, Btrie_fast_mgr core_trie) {}
public int Make_tkn(Xop_ctx ctx, Xop_tkn_mkr tkn_mkr, Xop_root_tkn root, byte[] src, int src_len, int bgn_pos, int cur_pos) {
ctx.Subs_add_and_stack(root, tkn_mkr.Vnt_eqgt(bgn_pos, cur_pos));
return cur_pos;
}
public static final byte[] Hook = new byte[] {Byte_ascii.Eq, Byte_ascii.Gt};
public static final Xop_vnt_lxr_eqgt _ = new Xop_vnt_lxr_eqgt(); Xop_vnt_lxr_eqgt() {}
}
class Xop_vnt_lxr_bgn implements Xop_lxr {
public byte Lxr_tid() {return Xop_lxr_.Tid_vnt_bgn;}
public void Init_by_wiki(Xowe_wiki wiki, Btrie_fast_mgr core_trie) {core_trie.Add(Xop_vnt_lxr_.Hook_bgn, this);}
public void Init_by_lang(Xol_lang lang, Btrie_fast_mgr core_trie) {}
public int Make_tkn(Xop_ctx ctx, Xop_tkn_mkr tkn_mkr, Xop_root_tkn root, byte[] src, int src_len, int bgn_pos, int cur_pos) {
ctx.Subs_add_and_stack(root, tkn_mkr.Vnt(bgn_pos, cur_pos));
return cur_pos;
}
public static final Xop_vnt_lxr_bgn _ = new Xop_vnt_lxr_bgn(); Xop_vnt_lxr_bgn() {}
}
class Xop_vnt_lxr_end implements Xop_lxr {
private Xop_vnt_flag_parser flag_parser;
private Xop_vnt_rules_parser rule_parser;
public byte Lxr_tid() {return Xop_lxr_.Tid_vnt_end;}
public void Init_by_wiki(Xowe_wiki wiki, Btrie_fast_mgr core_trie) {
core_trie.Add(Xop_vnt_lxr_.Hook_end, this);
Xol_vnt_mgr vnt_mgr = wiki.Lang().Vnt_mgr();
flag_parser = new Xop_vnt_flag_parser(vnt_mgr);
rule_parser = new Xop_vnt_rules_parser(vnt_mgr);
}
public void Init_by_lang(Xol_lang lang, Btrie_fast_mgr core_trie) {}
public int Make_tkn(Xop_ctx ctx, Xop_tkn_mkr tkn_mkr, Xop_root_tkn root, byte[] src, int src_len, int bgn_pos, int cur_pos) {
int stack_pos = ctx.Stack_idx_typ(Xop_tkn_itm_.Tid_vnt);
if (stack_pos == Xop_ctx.Stack_not_found) return ctx.Lxr_make_txt_(cur_pos); // "}-" found but no "-{" in stack;
Xop_vnt_tkn vnt_tkn = (Xop_vnt_tkn)ctx.Stack_pop_til(root, src, stack_pos, false, bgn_pos, cur_pos, Xop_tkn_itm_.Tid_vnt);
Xowe_wiki wiki = ctx.Wiki();
try {
vnt_tkn.Src_end_(cur_pos);
vnt_tkn.Subs_move(root);
Xop_vnt_flag[] vnt_flag_ary = Xop_vnt_flag_.Ary_empty;
int rule_subs_bgn = 0;
int pipe_tkn_count = vnt_tkn.Vnt_pipe_tkn_count();
if (pipe_tkn_count > 0) {
flag_parser.Parse(wiki, vnt_tkn, pipe_tkn_count, src);
vnt_flag_ary = flag_parser.Rslt_flags();
rule_subs_bgn = flag_parser.Rslt_tkn_pos();
vnt_tkn.Vnt_pipe_idx_last_(flag_parser.Rslt_pipe_last());
}
vnt_tkn.Vnt_flags_(vnt_flag_ary);
Xop_vnt_rule[] rules = rule_parser.Parse(ctx, vnt_tkn, src, rule_subs_bgn);
vnt_tkn.Vnt_rules_(rules);
vnt_tkn.Vnt_cmd_calc(wiki, ctx.Cur_page(), ctx, src);
}
catch (Exception e) {
ctx.App().Usr_dlg().Warn_many("", "", "vnt.parse failed: page=~{0} src=~{1} err=~{2}", String_.new_u8(ctx.Cur_page().Ttl().Raw()), String_.new_u8(src, bgn_pos, cur_pos), Err_.Message_gplx_brief(e));
if (vnt_tkn != null)
root.Subs_add(tkn_mkr.Bry_mid(src, vnt_tkn.Src_bgn(), cur_pos));
}
return cur_pos;
}
}

View File

@@ -0,0 +1,163 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import org.junit.*;
public class Xop_vnt_lxr_tst {
private Xop_vnt_lxr_fxt fxt = new Xop_vnt_lxr_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Flag_unknown() {fxt.Test_parse("-{X|b}-" , fxt.vnt_().Flags_unknown_().Rule_("b"));}
@Test public void Flag_raw_basic() {fxt.Test_parse("-{A|b}-" , fxt.vnt_().Flags_codes_("A").Rule_("b"));}
@Test public void Flag_add_ws() {fxt.Test_parse("-{ A |b}-" , fxt.vnt_().Flags_codes_("A").Rule_("b"));}
@Test public void Flag_add_unknown() {fxt.Test_parse("-{ A x |b}-" , fxt.vnt_().Flags_unknown_().Rule_("b"));}
@Test public void Flag_langs_basic() {fxt.Test_parse("-{zh-hans;zh-hant|b}-" , fxt.vnt_().Flags_langs_("zh-hans", "zh-hant").Rule_("b"));}
@Test public void Flag_langs_semic() {fxt.Test_parse("-{zh-hans;zh-hant;|b}-" , fxt.vnt_().Flags_langs_("zh-hans", "zh-hant").Rule_("b"));}
@Test public void Flag_langs_ws() {fxt.Test_parse("-{ zh-hans ; zh-hant ; |b}-" , fxt.vnt_().Flags_langs_("zh-hans", "zh-hant").Rule_("b"));}
@Test public void Flag_unknown_1st() {fxt.Test_parse("-{ zh-hans x ; zh-hant ; |b}-" , fxt.vnt_().Flags_unknown_().Rule_("b"));}
@Test public void Flag_unknown_nth() {fxt.Test_parse("-{ zh-hans ; zh-hant x; |b}-" , fxt.vnt_().Flags_unknown_().Rule_("b"));}
@Test public void Flag_unknown_all() {fxt.Test_parse("-{ zh-hans x ; zh-hant x;|b}-" , fxt.vnt_().Flags_unknown_().Rule_("b"));}
@Test public void Flag_multiple() {fxt.Test_parse("-{A|D|E|b}-" , fxt.vnt_().Flags_codes_("A", "D", "E").Rule_("b"));}
@Test public void Rule_add_one() {fxt.Test_parse("-{A|zh-hans:bcd}-" , fxt.vnt_().Flags_codes_("A").Rule_("zh-hans", "bcd"));}
@Test public void Rule_add_one_semic() {fxt.Test_parse("-{A|zh-hans:bcd;}-" , fxt.vnt_().Flags_codes_("A").Rule_("zh-hans", "bcd"));}
@Test public void Rule_add_one_semic_empty() {fxt.Test_parse("-{A|zh-hans:bcd;;}-" , fxt.vnt_().Flags_codes_("A").Rule_("zh-hans", "bcd"));}
@Test public void Rule_add_one_ws() {fxt.Test_parse("-{A|zh-hans : b c ;}-" , fxt.vnt_().Flags_codes_("A").Rule_("zh-hans", "b c"));}
@Test public void Rule_add_many() {fxt.Test_parse("-{A|zh-hans:b;zh-hant:c}-" , fxt.vnt_().Flags_codes_("A").Rule_("zh-hans", "b").Rule_("zh-hant", "c"));}
@Test public void Macro_one() {fxt.Test_parse("-{H|A1=>zh-hans:b;zh-hant:c}-" , fxt.vnt_().Flags_codes_("H").Rule_("A1", "zh-hans", "b").Rule_("A1", "zh-hant", "c"));}
@Test public void Bidi() {fxt.Test_parse("-{zh-hans:b;zh-hant:c}-" , fxt.vnt_().Flags_none_().Rule_("zh-hans", "b").Rule_("zh-hant", "c"));}
@Test public void None() {fxt.Test_parse("-{a}-" , fxt.vnt_().Flags_none_().Rule_("a"));}
@Test public void Macro_mult() {
fxt.Test_parse("-{H|A1=>zh-hans:b;zh-hant:c;A2=>zh-hans:d;zh-hant:e}-"
, fxt.vnt_().Flags_codes_("H")
.Rule_("A1", "zh-hans", "b").Rule_("A1", "zh-hant", "c")
.Rule_("A2", "zh-hans", "d").Rule_("A2", "zh-hant", "e")
);
}
// @Test public void Disabled() {
// Xop_fxt fxt = new Xop_fxt();
// fxt.Wiki().Vnt_mgr().Set(null, null);
// fxt.Test_parse_page_all_str("a-{b}-c", "a-{b}-c");
// }
// @Test public void Enabled() {
// Xoae_app app = Xoa_app_fxt.app_();
// Xol_lang lang = new Xol_lang(app, Bry_.new_a7("zh"));
// Xowe_wiki wiki = Xoa_app_fxt.wiki_(app, "zh.wikipedia.org", lang);
// Xop_fxt fxt = new Xop_fxt(app, wiki);
// fxt.Test_parse_page_all_str("a-{b}-c", "ac");
// fxt.Wiki().Vnt_mgr().Set(null, null); // set it back to null for other tests
// }
}
class Xop_vnt_tkn_mok {
private List_adp rules_list = List_adp_.new_();
private List_adp flags_list = List_adp_.new_();
public Xop_vnt_flag[] Flags() {
if (flags == null) flags = (Xop_vnt_flag[])flags_list.To_ary(Xop_vnt_flag.class);
return flags;
} private Xop_vnt_flag[] flags;
public Xop_vnt_tkn_mok Flags_none_() {flags_list.Clear(); return this;}
public Xop_vnt_tkn_mok Flags_unknown_(String... v) {flags_list.Add(Xop_vnt_flag_.Flag_unknown); return this;}
public Xop_vnt_tkn_mok Flags_langs_(String... v) {flags_list.Add(Xop_vnt_flag_.new_langs_(Bry_.Ary(v))); return this;}
public Xop_vnt_tkn_mok Flags_codes_(String... ary) {
int len = ary.length;
for (int i = 0; i < len; i++) {
byte[] bry = Bry_.new_a7(ary[i]);
Xop_vnt_flag flag = (Xop_vnt_flag)Xop_vnt_flag_.Trie.Match_bgn(bry, 0, bry.length);
flags_list.Add(flag);
}
return this;
}
public Xop_vnt_rule[] Rules() {
if (rules == null) rules = (Xop_vnt_rule[])rules_list.To_ary(Xop_vnt_rule.class);
return rules;
} private Xop_vnt_rule[] rules;
public Xop_vnt_tkn_mok Rule_(String rule) {return Rule_(Xop_vnt_rule.Null_lang, rule);}
public Xop_vnt_tkn_mok Rule_(byte[] lang, String rule) {return Rule_(Xop_vnt_rule.Null_macro, lang, new Xop_bry_tkn(-1, -1, Bry_.new_u8(rule)));}
public Xop_vnt_tkn_mok Rule_(String lang, String rule) {return Rule_(Xop_vnt_rule.Null_macro, Bry_.new_a7(lang), new Xop_bry_tkn(-1, -1, Bry_.new_u8(rule)));}
public Xop_vnt_tkn_mok Rule_(String macro, String lang, String rule) {return Rule_(Bry_.new_a7(macro), Bry_.new_a7(lang), new Xop_bry_tkn(-1, -1, Bry_.new_u8(rule)));}
public Xop_vnt_tkn_mok Rule_(byte[] macro, byte[] lang, Xop_tkn_itm... tkns) {rules_list.Add(new Xop_vnt_rule(macro, lang, tkns)); return this;}
}
class Xop_vnt_lxr_fxt {
private Xop_fxt fxt;
private Bry_bfr tmp_bfr = Bry_bfr.new_();
public Xop_vnt_lxr_fxt Clear() {
Xoae_app app = Xoa_app_fxt.app_();
Xowe_wiki wiki = Xoa_app_fxt.wiki_(app, "zh.wikipedia.org");
fxt = new Xop_fxt(app, wiki);
Xop_vnt_lxr_fxt.Init_vnt_mgr(wiki.Lang().Vnt_mgr(), "zh-hans", "zh-hant");
Xop_vnt_lxr_.set_(wiki);
return this;
}
public Xop_vnt_tkn_mok vnt_() {return new Xop_vnt_tkn_mok();}
public static void Init_vnt_mgr(Xol_vnt_mgr vnt_mgr, String... vnts_str) {
byte[][] vnts_bry = Bry_.Ary(vnts_str);
int vnts_bry_len = vnts_bry.length;
for (int i = 0; i < vnts_bry_len; i++)
vnt_mgr.Get_or_new(vnts_bry[i]);
vnt_mgr.Convert_ttl_init();
}
public Xop_vnt_lxr_fxt Test_parse(String raw, Xop_vnt_tkn_mok expd) {
byte[] raw_bry = Bry_.new_u8(raw);
Xop_root_tkn root = fxt.Exec_parse_page_all_as_root(raw_bry);
Xop_vnt_tkn actl = (Xop_vnt_tkn)root.Subs_get(0);
Test_vnt_tkn(raw_bry, expd, actl);
return this;
}
private void Test_vnt_tkn(byte[] raw_bry, Xop_vnt_tkn_mok expd, Xop_vnt_tkn actl) {
Tfds.Eq(Vnt_flag_ary_to_str(tmp_bfr, expd.Flags()), Vnt_flag_ary_to_str(tmp_bfr, actl.Vnt_flags()), "flags");
Tfds.Eq(Vnt_rule_ary_to_str(tmp_bfr, raw_bry, expd.Rules()), Vnt_rule_ary_to_str(tmp_bfr, raw_bry, actl.Vnt_rules()), "rules");
}
private String Vnt_flag_ary_to_str(Bry_bfr bfr, Xop_vnt_flag[] ary) {
int len = ary.length;
for (int i = 0; i < len; i++) {
Xop_vnt_flag itm = ary[i];
byte itm_tid = itm.Tid();
if (itm_tid == Xop_vnt_flag_.Tid_lang)
Vnt_flag_lang_to_bfr(bfr, itm);
else
bfr.Add_str(Xop_vnt_flag_.Xto_name(itm_tid)).Add_byte(Byte_ascii.Semic);
}
return bfr.Xto_str_and_clear();
}
private void Vnt_flag_lang_to_bfr(Bry_bfr bfr, Xop_vnt_flag itm) {
byte[][] ary = itm.Langs();
int len = ary.length;
for (int i = 0; i < len; i++)
bfr.Add(ary[i]).Add_byte(Byte_ascii.Semic);
}
private String Vnt_rule_ary_to_str(Bry_bfr bfr, byte[] src, Xop_vnt_rule[] ary) {
if (ary == null) return "";
int len = ary.length;
for (int i = 0; i < len; i++) {
Xop_vnt_rule itm = ary[i];
if (itm.Rule_macro() != Xop_vnt_rule.Null_macro) // macro exists
bfr.Add(itm.Rule_macro()).Add_str("=>");
if (itm.Rule_lang() != Xop_vnt_rule.Null_lang) // lang exists
bfr.Add(itm.Rule_lang()).Add_byte(Byte_ascii.Colon);
Xop_tkn_itm[] subs = itm.Rule_subs();
int subs_len = subs.length;
for (int j = 0; j < subs_len; j++) {
Xop_tkn_itm sub = subs[j];
if (sub.Tkn_tid() == Xop_tkn_itm_.Tid_bry) // tests uses Xop_tkn_bry
bfr.Add(((Xop_bry_tkn)sub).Val());
else
bfr.Add_mid(src, sub.Src_bgn(), sub.Src_end());
}
bfr.Add_byte(Byte_ascii.Semic);
}
return bfr.Xto_str_and_clear();
}
}

View File

@@ -0,0 +1,88 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import org.junit.*;
public class Xop_vnt_parser_tst { // uses zh-hant as cur_vnt
private Xop_vnt_parser_fxt fxt = new Xop_vnt_parser_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Literal() {fxt.Test_parse("-{A}-", "A");}
@Test public void Bidi() {fxt.Test_parse("-{zh-hans:A;zh-hant:B}-", "B");}
@Test public void Empty() {fxt.Test_parse("a-{}-b", "ab");}
@Test public void Unknown_empty() {fxt.Test_parse("a-{|}-c", "ac");}
@Test public void Unknown_text() {fxt.Test_parse("a-{|b}-c", "abc");}
@Test public void Unknown_flag() {fxt.Test_parse("a-{x|b}-c", "abc");}
@Test public void Lang_y() {fxt.Test_parse("-{zh-hant|A}-", "A");}
@Test public void Lang_n() {fxt.Test_parse("-{zh-hans|A}-", "");}
@Test public void Raw() {fxt.Test_parse("-{R|zh-hans:A;}-", "zh-hans:A;");}
// @Test public void Descrip() {fxt.Test_parse("-{D|zh-hans:A;}-", "zh-hans:A");}
@Test public void Tmpl() {
fxt.Parser_fxt().Init_page_create("Template:A", "B");
fxt.Test_parse("-{{{A}}}-", "B");
}
@Test public void Tmpl_arg_4() { // PURPOSE: handle "-{" + "{{{"
fxt.Parser_fxt().Init_page_create("Template:A", "-{{{{1}}}}-");
fxt.Test_parse("{{A|B}}", "B"); // -{ {{{1}}} }- -> -{B}- -> B
}
@Test public void Tmpl_arg_3() { // PURPOSE: handle "-" + "{{{"; PAGE:sr.w:ДНК; EX:<span id="interwiki-{{{1}}}-fa"></span> DATE:2014-07-03
fxt.Parser_fxt().Init_page_create("Template:A", "-{{{1}}}-");
fxt.Test_parse("{{A|B}}", "-B-");
}
@Test public void Parser_function() {
fxt.Test_parse("-{{{#expr:1}}}-", "1");
}
@Test public void Ignore() {
fxt.Test_parse("-{{#expr:1}}-", "-1-");
}
@Test public void Expr() {
fxt.Parser_fxt().Init_page_create("Template:A", "{{#expr: 0-{{{1|2}}}}}");
fxt.Test_parse("{{A}}", "-2");
}
@Test public void Invalid() { // PURPOSE: invalid flags should cause vnt to render text only; DATE:2014-04-10
fxt.Test_parse("-{:a|b}-", "b");
}
@Test public void Macro_ignore() { // PURPOSE: ignore macro (implement later); EX:zh.v:西安; Template:pagebanner; DATE:2014-05-03
fxt.Test_parse("-{H|zh-cn:亚琛; zh-tw:阿亨;}-", "");
}
@Test public void Title() { // PURPOSE: implement title; PAGE:zh.w:Help:進階字詞轉換處理 DATE:2014-08-29
fxt.Test_parse("-{T|zh-hant:A;zh-hans:B}-", "");
Tfds.Eq("A", String_.new_u8(fxt.Parser_fxt().Page().Html_data().Display_ttl_vnt()));
}
}
class Xop_vnt_parser_fxt {
public Xop_fxt Parser_fxt() {return fxt;} private Xop_fxt fxt;
public Xop_vnt_parser_fxt Clear() {
Xoae_app app = Xoa_app_fxt.app_();
Xowe_wiki wiki = Xoa_app_fxt.wiki_(app, "zh.wikipedia.org");
fxt = new Xop_fxt(app, wiki);
Init_vnt_mgr(wiki.Lang().Vnt_mgr(), "zh-hans", "zh-hant");
Xop_vnt_lxr_.set_(wiki);
wiki.Lang().Vnt_mgr().Cur_vnt_(Bry_.new_a7("zh-hant"));
return this;
}
private static void Init_vnt_mgr(Xol_vnt_mgr vnt_mgr, String... vnts_str) {
byte[][] vnts_bry = Bry_.Ary(vnts_str);
int vnts_bry_len = vnts_bry.length;
for (int i = 0; i < vnts_bry_len; i++)
vnt_mgr.Get_or_new(vnts_bry[i]);
vnt_mgr.Convert_ttl_init();
}
public Xop_vnt_parser_fxt Test_parse(String raw, String expd) {
fxt.Test_parse_page_all_str(raw, expd);
return this;
}
}

View File

@@ -0,0 +1,26 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
public class Xop_vnt_rule extends Xop_tkn_itm_base {
public Xop_vnt_rule(byte[] rule_macro, byte[] rule_lang, Xop_tkn_itm[] rule_subs) {this.rule_macro = rule_macro; this.rule_lang = rule_lang; this.rule_subs = rule_subs;}
@Override public byte Tkn_tid() {return Xop_tkn_itm_.Tid_vnt_rule;}
public byte[] Rule_macro() {return rule_macro;} private byte[] rule_macro;
public byte[] Rule_lang() {return rule_lang;} private byte[] rule_lang;
public Xop_tkn_itm[] Rule_subs() {return rule_subs;} private Xop_tkn_itm[] rule_subs;
public static final byte[] Null_lang = null, Null_macro = null;
}

View File

@@ -0,0 +1,229 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.core.btries.*;
class Xop_vnt_rules_parser {
private byte mode;
private Xop_vnt_tkn vnt_tkn;
private boolean loop_vnt_subs; private int vnt_subs_cur, vnt_subs_bgn, vnt_subs_len;
private int rule_texts_bgn;
private Btrie_slim_mgr trie;
private List_adp rules_list = List_adp_.new_();
private List_adp text_tkns_list = List_adp_.new_();
private int text_tkns_ws_end_idx;
private byte[] src;
private Xop_tkn_mkr tkn_mkr;
// private int cur_macro_bgn = -1;
private int cur_key_bgn = -1;
private byte[] cur_macro_bry = null;
private byte[] cur_lang_bry = null;
public Xop_vnt_rules_parser(Xol_vnt_mgr vnt_mgr) {
trie = Btrie_slim_mgr.ci_ascii_(); // NOTE:ci.ascii:MW_const.en; lang variant name; EX:zh-hans
Xol_vnt_converter[] ary = vnt_mgr.Converter_ary();
int ary_len = ary.length;
for (int i = 0; i < ary_len; i++) {
Xol_vnt_converter itm = ary[i];
byte[] itm_lang = itm.Owner_key();
trie.Add_obj(itm_lang, Xop_vnt_rule_trie_itm.lang_(itm_lang));
}
trie.Add_obj(";", Xop_vnt_rule_trie_itm.Dlm_semic);
// trie.Add("=>", Xop_vnt_rule_trie_itm.Dlm_eqgt);
}
public void Clear_all() {
rules_list.Clear();
text_tkns_list.Clear();
cur_macro_bry = cur_lang_bry = null;
cur_key_bgn = -1;
text_tkns_ws_bgn = false;
text_tkns_ws_end_idx = 0;
}
public Xop_vnt_rule[] Parse(Xop_ctx ctx, Xop_vnt_tkn vnt_tkn, byte[] src, int vnt_subs_bgn) {
this.Clear_all();
this.tkn_mkr = ctx.Tkn_mkr(); this.src = src;
this.vnt_tkn = vnt_tkn;
this.vnt_subs_len = vnt_tkn.Subs_len();
this.vnt_subs_bgn = this.vnt_subs_cur = vnt_subs_bgn;
mode = Mode_key;
loop_vnt_subs = true;
while (loop_vnt_subs) {
if (vnt_subs_cur == vnt_subs_len) break;
Parse_sub();
++vnt_subs_cur;
}
Make_rule(); // make rules for any pending items; EX: "-{A|text}-"; "text" is unclosed by semic and would need to be processed
if (mode == Mode_key && rules_list.Count() == 0)
Make_rule_literal();
return (Xop_vnt_rule[])rules_list.To_ary_and_clear(Xop_vnt_rule.class);
}
private boolean text_tkns_ws_bgn = false;
private void Parse_sub() {
Xop_tkn_itm sub = vnt_tkn.Subs_get(vnt_subs_cur);
if (cur_key_bgn == -1) cur_key_bgn = sub.Src_bgn();
boolean text_tkns_list_add = true;
boolean sub_is_ws = false;
switch (sub.Tkn_tid()) {
case Xop_tkn_itm_.Tid_txt:
Parse_key(sub, src, sub.Src_bgn(), sub.Src_end());
if (mode == Mode_key)
text_tkns_list_add = false;
break;
case Xop_tkn_itm_.Tid_bry:
Xop_bry_tkn bry_tkn = (Xop_bry_tkn)sub;
byte[] bry = bry_tkn.Val();
Parse_key(sub, bry, 0, bry.length);
break;
case Xop_tkn_itm_.Tid_colon:
if ( mode == Mode_lang // colon should only follow lang; EX: zh-hant:text
&& cur_lang_bry == null) { // if pending lang, ignore; assume part of text
cur_lang_bry = Bry_.Trim(Bry_.Mid(src, cur_key_bgn, sub.Src_bgn()));
mode = Mode_text;
rule_texts_bgn = vnt_subs_cur + 1;
text_tkns_list_add = false;
text_tkns_ws_bgn = true;
}
break;
case Xop_tkn_itm_.Tid_space:
case Xop_tkn_itm_.Tid_tab:
case Xop_tkn_itm_.Tid_newLine: // skip ws
if (text_tkns_ws_bgn) text_tkns_list_add = false;
else sub_is_ws = true;
break;
case Xop_tkn_itm_.Tid_vnt_eqgt:
if (mode == Mode_key) {
cur_macro_bry = Bry_.Trim(Bry_.Mid(src, cur_key_bgn, sub.Src_bgn()));
text_tkns_list_add = false;
}
break;
default:
break;
}
if (sub_is_ws)
text_tkns_ws_end_idx = text_tkns_list.Count(); // update last_ws_idx
else
text_tkns_ws_end_idx = -1; // set last_ws_idx to nil
if (mode == Mode_text && text_tkns_list_add) { // mode is text && not a "key" tkn
text_tkns_list.Add(sub);
text_tkns_ws_bgn = false; // mark ws_bgn as false (handles trimming at start)
}
}
private void Parse_key(Xop_tkn_itm sub, byte[] src, int src_bgn, int src_end) {
int pos = src_bgn;
boolean loop_key_bry = true;
// boolean rv_add_as_text_tkn = true;
while (loop_key_bry) {
if (pos == src_end) break;
if (cur_key_bgn == -1) cur_key_bgn = pos;
byte b = src[pos];
Object itm_obj = trie.Match_bgn_w_byte(b, src, pos, src_end);
if (itm_obj == null) { // not a lang, semic, or eqgt; treat rest of vnt as one rule tkn
// if (mode == Mode_key)
// loop_key_bry = Make_rule_literal();
// else
++pos;
}
else {
Xop_vnt_rule_trie_itm itm = (Xop_vnt_rule_trie_itm)itm_obj;
int new_pos = trie.Match_pos();
switch (itm.Tid()) {
case Xop_vnt_rule_trie_itm.Tid_lang:
if (mode == Mode_key) {
int next_char_pos = Bry_finder.Find_fwd_while_space_or_tab(src, new_pos, src_end);
if (next_char_pos == src_end) { // eos; EX: "zh-hant :a"
cur_key_bgn = pos;
mode = Mode_lang;
}
else
loop_key_bry = Make_rule_literal();
return;
}
break;
case Xop_vnt_rule_trie_itm.Tid_semic:
switch (mode) {
case Mode_text:
text_tkns_list.Add(tkn_mkr.Bry_raw(src_bgn, pos, Bry_.Trim(Bry_.Mid(src, src_bgn, pos))));
Make_rule();
cur_lang_bry = null;
mode = Mode_key;
cur_key_bgn = -1;
break;
case Mode_key: // ignore; empty semic's; EX: "zh-hant:a;;"
break;
}
// rv_add_as_text_tkn = false;
break;
// case Xop_vnt_rule_trie_itm.Tid_eqgt:
// if ( mode == Mode_key) { // if pending lang, ignore; assume part of text
// cur_macro_bry = Bry_.Mid(src, cur_key_bgn, sub.Src_bgn());
// cur_key_bgn = new_pos + 1;
// }
// break;
}
pos = new_pos;
}
}
// if (rv_add_as_text_tkn)
// mode = Mode_text;
// return rv_add_as_text_tkn;
}
private boolean Make_rule_literal() { // return false for loop_key_bry
this.Clear_all();
Xop_tkn_itm[] rule_subs = Make_subs(vnt_subs_bgn, vnt_subs_len);
rules_list.Add(new Xop_vnt_rule(Xop_vnt_rule.Null_macro, Xop_vnt_rule.Null_lang, rule_subs));
loop_vnt_subs = false;
return false;
}
private Xop_tkn_itm[] Make_subs(int bgn, int end) {
int len = end - bgn;
Xop_tkn_itm[] rv = new Xop_tkn_itm[len];
for (int i = bgn; i < end; i++)
rv[i - bgn] = vnt_tkn.Subs_get(i);
return rv;
}
private void Make_rule() {
if ( mode == Mode_text
&& rule_texts_bgn < vnt_subs_len) {
if (text_tkns_ws_end_idx != -1) { // trailing ws
text_tkns_list.Del_range(text_tkns_ws_end_idx, text_tkns_list.Count() - 1);
}
Xop_tkn_itm[] rule_subs = (Xop_tkn_itm[])text_tkns_list.To_ary_and_clear(Xop_tkn_itm.class);
Xop_vnt_rule rule = new Xop_vnt_rule(cur_macro_bry, cur_lang_bry, rule_subs);
rules_list.Add(rule);
}
}
private static final byte Mode_key = 1, Mode_lang = 2, Mode_text = 3;//, Mode_macro = 4;
}
class Xop_vnt_rule_trie_itm {
public Xop_vnt_rule_trie_itm(byte tid, byte[] lang) {this.tid = tid; this.lang = lang;}
public byte Tid() {return tid;} private byte tid;
public byte[] Lang() {return lang;} private byte[] lang;
public static final byte Tid_semic = 1, Tid_lang = 2;
public static Xop_vnt_rule_trie_itm lang_(byte[] lang) {return new Xop_vnt_rule_trie_itm(Tid_lang, lang);}
public static final Xop_vnt_rule_trie_itm
Dlm_semic = new Xop_vnt_rule_trie_itm(Tid_semic, null)
;
}
/*
-{flags|lang:rule}- EX: -{A|zh-hant:a}-
-{lang:rule;lang:rule} EX: -{zh-hans:a;zh-hant:b}-
-{lang;lang|rule}- EX: -{zh-hans;zh-hant|XXXX}-
-{rule}- EX: -{a}-
-{flags|from=>variant:to;}- EX: -{H|HUGEBLOCK=>zh-cn:macro;}-
-{lang:data_0;data_1;}- EX: -{zh-hans:<span style='border:solid;color:blue;'>;zh-hant:b}-
. where data_0 and data_1 is actually one itm since ; is not delimiter b/c data_1 must be variant_code
-{zh-hans:a-{zh-hans:b}-c}-
*/

View File

@@ -0,0 +1,70 @@
/*
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.langs.vnts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
import gplx.xowa.html.*;
public class Xop_vnt_tkn extends Xop_tkn_itm_base {
public Xop_vnt_tkn(int bgn, int end) {
this.Tkn_ini_pos(false, bgn, end);
vnt_pipe_idx_last = bgn + Xop_vnt_lxr_.Hook_bgn.length; // default last pipe to pos after -{
}
@Override public byte Tkn_tid() {return Xop_tkn_itm_.Tid_vnt;}
public int Vnt_pipe_tkn_count() {return vnt_pipe_tkn_count;}
public Xop_vnt_tkn Vnt_pipe_tkn_count_add_() {++vnt_pipe_tkn_count; return this;} private int vnt_pipe_tkn_count;
public int Vnt_pipe_idx_last() {return vnt_pipe_idx_last;} public Xop_vnt_tkn Vnt_pipe_idx_last_(int v) {vnt_pipe_idx_last = v; return this;} private int vnt_pipe_idx_last = -1;
public Xop_vnt_flag[] Vnt_flags() {return vnt_flags;} public Xop_vnt_tkn Vnt_flags_(Xop_vnt_flag[] v) {vnt_flags = v; return this;} private Xop_vnt_flag[] vnt_flags;
public Xop_vnt_rule[] Vnt_rules() {return vnt_rules;} public Xop_vnt_tkn Vnt_rules_(Xop_vnt_rule[] v) {vnt_rules = v; return this;} private Xop_vnt_rule[] vnt_rules;
public byte Vnt_cmd() {return vnt_cmd;} private byte vnt_cmd;
public void Vnt_cmd_calc(Xowe_wiki wiki, Xoae_page page, Xop_ctx ctx, byte[] src) {
int flags_len = vnt_flags.length;
int rules_len = vnt_rules.length;
if (flags_len == 0) { // no flags; either literal ("-{A}-") or bidi ("-{zh-hans:A;zh-hant:B}-");
if (rules_len == 0) vnt_cmd = Xop_vnt_html_wtr.Cmd_empty;
else {
Xop_vnt_rule rule_0 = vnt_rules[0];
if ( rules_len == 1 // only one rule
&& rule_0.Rule_lang() == Xop_vnt_rule.Null_lang // no lang; EX: -{A}-
)
vnt_cmd = Xop_vnt_html_wtr.Cmd_literal;
else // bidi: either one rule which has lang ("-{zh-hans:A}-") or more than one rule (which can't be literal)
vnt_cmd = Xop_vnt_html_wtr.Cmd_bidi;
}
}
else if (flags_len == 1){ // 1 flag; common case
Xop_vnt_flag flag_0 = vnt_flags[0];
switch (flag_0.Tid()) {
case Xop_vnt_flag_.Tid_lang : vnt_cmd = Xop_vnt_html_wtr.Cmd_lang; break;
case Xop_vnt_flag_.Tid_raw : vnt_cmd = Xop_vnt_html_wtr.Cmd_raw; break;
case Xop_vnt_flag_.Tid_descrip : vnt_cmd = Xop_vnt_html_wtr.Cmd_descrip; break;
case Xop_vnt_flag_.Tid_unknown : vnt_cmd = Xop_vnt_html_wtr.Cmd_literal; break; // flag is unknown; output text as literal; EX: "-{|a}-"; "-{X|a}-"
case Xop_vnt_flag_.Tid_macro : vnt_cmd = Xop_vnt_html_wtr.Cmd_empty; break; // TODO: implement macro; ignore for now; DATE:2014-05-03
case Xop_vnt_flag_.Tid_title: { // title; same as {{DISPLAYTITLE}} but variant aware; PAGE:zh.w:Help:進階字詞轉換處理 DATE:2014-08-29
vnt_cmd = Xop_vnt_html_wtr.Cmd_title;
byte[] cur_lang_vnt = wiki.Lang().Vnt_mgr().Cur_vnt();
Xop_vnt_rule rule = Xop_vnt_html_wtr.Get_rule_by_key(vnt_rules, vnt_rules.length, cur_lang_vnt);
if (rule != null) {
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_b512();
wiki.Html_mgr().Html_wtr().Write_tkn_ary(tmp_bfr, ctx, Xoh_wtr_ctx.Alt, src, rule.Rule_subs());
byte[] display_ttl = tmp_bfr.To_bry_and_rls();
page.Html_data().Display_ttl_vnt_(display_ttl);
}
break;
}
}
}
}
}