mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.9.3.1
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
public class Xol_convert_grp implements GfoInvkAble {// group of convert_itm by vnt; EX: zh-hant {A -> A1; B -> B1}
|
||||
private final Ordered_hash hash = Ordered_hash_.new_bry_();
|
||||
public Xol_convert_grp(byte[] key) {this.key = key;}
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public int Len() {return hash.Count();}
|
||||
public Xol_convert_itm Get_at(int i) {return (Xol_convert_itm)hash.Get_at(i);}
|
||||
public void Add(byte[] src, byte[] trg) {hash.Add_if_dupe_use_nth(src, new Xol_convert_itm(src, trg));}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_add_bulk)) Add_bulk(this, m.ReadBry("v"));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_add_bulk = "add_bulk";
|
||||
private static void Add_bulk(Xol_convert_grp grp, byte[] raw) {
|
||||
int len = raw.length;
|
||||
int pos = 0, fld_bgn = 0, fld_idx = 0;
|
||||
byte[] src = Bry_.Empty, trg = Bry_.Empty;
|
||||
Xol_csv_parser csv_parser = Xol_csv_parser._;
|
||||
while (true) {
|
||||
boolean last = pos == len;
|
||||
byte b = last ? Byte_ascii.Nl : raw[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Pipe:
|
||||
switch (fld_idx) {
|
||||
case 0: src = csv_parser.Load(raw, fld_bgn, pos); break;
|
||||
default: throw Err_.new_unhandled(fld_idx);
|
||||
}
|
||||
fld_bgn = pos + 1;
|
||||
++fld_idx;
|
||||
break;
|
||||
case Byte_ascii.Nl:
|
||||
if (fld_bgn < pos) { // guard against trailing new lines
|
||||
trg = csv_parser.Load(raw, fld_bgn, pos);
|
||||
grp.Add(src, trg);
|
||||
}
|
||||
fld_bgn = pos + 1;
|
||||
fld_idx = 0;
|
||||
break;
|
||||
}
|
||||
if (last) break;
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
public class Xol_convert_itm {
|
||||
public Xol_convert_itm(byte[] src, byte[] trg) {this.src = src; this.trg = trg;} // convert from src to trg; EX: A -> A1
|
||||
public byte[] Src() {return src;} private final byte[] src;
|
||||
public byte[] Trg() {return trg;} private final byte[] trg;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
import gplx.xowa.nss.*; import gplx.xowa.wikis.data.tbls.*;
|
||||
public class Xol_convert_mgr {
|
||||
private final Ordered_hash tmp_page_list = Ordered_hash_.new_bry_();
|
||||
private int wkr_ary_len, cur_wkr_idx = -1;
|
||||
public Xol_convert_regy Converter_regy() {return converter_regy;} private final Xol_convert_regy converter_regy = new Xol_convert_regy();
|
||||
public Xol_convert_wkr[] Converter_ary() {return wkr_ary;} private Xol_convert_wkr[] wkr_ary;
|
||||
public void Init(Xol_vnt_regy regy) {
|
||||
int len = regy.Len();
|
||||
this.wkr_ary_len = len;
|
||||
this.wkr_ary = new Xol_convert_wkr[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_vnt_itm itm = regy.Get_at(i);
|
||||
itm.Convert_wkr().Rebuild(converter_regy, itm.Convert_ary());
|
||||
wkr_ary[i] = itm.Convert_wkr();
|
||||
}
|
||||
}
|
||||
public void Cur_vnt_(byte[] cur_vnt) {
|
||||
int new_wkr_idx = -1;
|
||||
for (int i = 0; i < wkr_ary_len; i++) {
|
||||
Xol_convert_wkr wkr = wkr_ary[i];
|
||||
if (Bry_.Eq(cur_vnt, wkr.Key())) {
|
||||
new_wkr_idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (new_wkr_idx == -1) throw Err_.new_("lang.vnt", "unknown vnt", "key", cur_vnt);
|
||||
this.cur_wkr_idx = new_wkr_idx;
|
||||
}
|
||||
public byte[] Convert_text(Xowe_wiki wiki, byte[] src) {return Convert_text(wiki, src, 0, src.length);}
|
||||
public byte[] Convert_text(Xowe_wiki wiki, byte[] src, int bgn, int end) {
|
||||
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_m001();
|
||||
Xol_convert_wkr converter = wkr_ary[cur_wkr_idx];
|
||||
converter.Convert_text(tmp_bfr, src, bgn, end);
|
||||
return tmp_bfr.To_bry_and_rls();
|
||||
}
|
||||
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
|
||||
synchronized (tmp_page_list) {
|
||||
int converted = Convert_ttl__convert_each_vnt(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_each_vnt(Xowe_wiki wiki, Bry_bfr tmp_bfr, Xow_ns ns, byte[] ttl_bry) {
|
||||
synchronized (tmp_page_list) {
|
||||
tmp_page_list.Clear();
|
||||
int rv = 0;
|
||||
for (int i = 0; i < wkr_ary_len; i++) { // convert ttl for each variant
|
||||
Xol_convert_wkr converter = wkr_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
import gplx.xowa.apps.fsys.*;
|
||||
public class Xol_convert_regy implements GfoInvkAble { // registry of convert_grp; EX: zh-hans;zh-hant;
|
||||
private final Ordered_hash hash = Ordered_hash_.new_bry_();
|
||||
public Xol_convert_grp Get_or_null(byte[] key) {return (Xol_convert_grp)hash.Get_by(key);}
|
||||
public Xol_convert_grp Get_or_make(byte[] key) {
|
||||
Xol_convert_grp rv = (Xol_convert_grp)hash.Get_by(key);
|
||||
if (rv == null) {
|
||||
rv = new Xol_convert_grp(key);
|
||||
hash.Add(key, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_get)) return Get_or_make(m.ReadBry("v"));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
} private static final String Invk_get = "get";
|
||||
public static Io_url Bld_url(Xoa_fsys_mgr app_fsys_mgr, String lang) {return Bld_url(app_fsys_mgr.Cfg_lang_core_dir(), lang);}
|
||||
public static Io_url Bld_url(Io_url dir, String lang) {return dir.GenSubFil_nest("variants", lang + ".gfs");}
|
||||
}
|
||||
@@ -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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
import org.junit.*;
|
||||
import gplx.xowa.langs.vnts.*; import gplx.xowa.wikis.data.tbls.*;
|
||||
public class Xol_convert_regy_tst {
|
||||
private final Xol_convert_regy_fxt fxt = new Xol_convert_regy_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Basic() {
|
||||
fxt.Parser_fxt().Init_page_create("Template:Test_x1", "val");
|
||||
fxt.Parser_fxt().Test_parse_tmpl_str_test("{{Test_x0}}", "{{test}}", "val");
|
||||
}
|
||||
@Test public void Upper_1st() { // PURPOSE: convert should call Xoa_ttl.parse(), which will upper 1st letter; EX:{{jez-eng|sense}} -> Jez-eng; PAGE:sr.w:ДНК DATE:2014-07-06
|
||||
fxt.Parser_fxt().Init_page_create("Template:X1", "val");
|
||||
fxt.Parser_fxt().Test_parse_tmpl_str_test("{{x0}}", "{{test}}", "val");
|
||||
}
|
||||
@Test public void Redlink() { // PURPOSE: check redlink's Convert_ttl(Xowe_wiki wiki, Xoa_ttl ttl); DATE:2014-07-06
|
||||
fxt.Parser_fxt().Init_page_create("Template:Test_x1", "val");
|
||||
fxt.Test_convert_by_ttl("zh", "Template:Test_x0", Bool_.Y); // Template:Test_xo should not be parsed to Template:Template:Test_x0; EX:Шаблон:Šablon:Jez-eng; PAGE:sr.w:ДНК DATE:2014-07-06
|
||||
fxt.Test_convert_by_ttl("zh", "Template:Test_x1", Bool_.N); // note that convert of trg should not find title;
|
||||
fxt.Test_convert_by_ttl("zh", "Template:Test_x2", Bool_.N); // test that non-convert characters return false
|
||||
}
|
||||
@Test public void Pfunc() {
|
||||
fxt.Parser_fxt().Init_defn_clear();
|
||||
fxt.Parser_fxt().Init_page_create("Test_x1");
|
||||
fxt.Test_parse("{{#ifexist:Test_x0|y|n}}", "y");
|
||||
}
|
||||
}
|
||||
class Xol_convert_regy_fxt {
|
||||
public Xoae_app App() {return app;} private Xoae_app app;
|
||||
public Xowe_wiki Wiki() {return wiki;} private Xowe_wiki wiki;
|
||||
public Xop_fxt Parser_fxt() {return parser_fxt;} private Xop_fxt parser_fxt;
|
||||
public void Clear() {
|
||||
app = Xoa_app_fxt.app_();
|
||||
Xol_lang lang = app.Lang_mgr().Get_by_key_or_new(Bry_.new_a7("zh"));
|
||||
Xol_lang_.Lang_init(lang);
|
||||
Init_cnv(app, "zh", "zh-hant", KeyVal_.new_("x0", "x1"));
|
||||
wiki = Xoa_app_fxt.wiki_(app, "zh.wikipedia.org", lang);
|
||||
gplx.xowa.parsers.vnts.Xop_vnt_parser_fxt.Vnt_mgr__init(wiki.Lang().Vnt_mgr(), 1, String_.Ary("zh", "zh-hans", "zh-hant"));
|
||||
parser_fxt = new Xop_fxt(app, wiki);
|
||||
}
|
||||
public static void Init_cnv(Xoae_app app, String lang_key, String vnt_key, KeyVal... ary) {
|
||||
Xol_lang lang = app.Lang_mgr().Get_by_key_or_new(Bry_.new_a7(lang_key));
|
||||
Xol_convert_grp grp = lang.Vnt_mgr().Convert_mgr().Converter_regy().Get_or_make(Bry_.new_a7(vnt_key));
|
||||
int ary_len = ary.length;
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
KeyVal itm = ary[i];
|
||||
grp.Add(Bry_.new_u8(itm.Key()), Bry_.new_u8(itm.Val_to_str_or_empty()));
|
||||
}
|
||||
Xol_vnt_itm vnt_itm = lang.Vnt_mgr().Regy__get_or_new(Bry_.new_a7(vnt_key));
|
||||
vnt_itm.Convert_ary_(Bry_.Ary(vnt_key));
|
||||
vnt_itm.Convert_wkr().Rebuild(lang.Vnt_mgr().Convert_mgr().Converter_regy(), vnt_itm.Convert_ary());
|
||||
}
|
||||
// public void Test_convert(String lang, String vnt, String raw, String expd) {
|
||||
// Xol_convert_grp convert_grp = app.Lang_mgr().Get_by_key_or_new(Bry_.new_a7(lang)).Cnv_mgr().Get_or_new(Bry_.new_a7(vnt));
|
||||
// Bry_bfr bfr = Bry_bfr.new_();
|
||||
// boolean converted = convert_grp.Convert_to_bfr(bfr, Bry_.new_u8(raw));
|
||||
// String actl = converted ? bfr.Xto_str_and_clear() : raw;
|
||||
// Tfds.Eq(expd, actl);
|
||||
// }
|
||||
public void Test_parse(String raw, String expd) {
|
||||
parser_fxt.Test_parse_page_all_str(raw, expd);
|
||||
}
|
||||
public void Test_convert_by_ttl(String lang_key, String raw, boolean expd) {
|
||||
Xol_lang lang = app.Lang_mgr().Get_by_key_or_new(Bry_.new_a7(lang_key));
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, Bry_.new_u8(raw));
|
||||
Xowd_page_itm page = lang.Vnt_mgr().Convert_mgr().Convert_ttl(wiki, ttl);
|
||||
if (expd)
|
||||
Tfds.Eq_true(page.Exists());
|
||||
else
|
||||
Tfds.Eq_null(page);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
import gplx.core.btries.*; import gplx.core.intls.*;
|
||||
public class Xol_convert_wkr {
|
||||
private final Btrie_slim_mgr trie = Btrie_slim_mgr.cs();
|
||||
public Xol_convert_wkr(byte[] key) {this.key = key;}
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
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();
|
||||
}
|
||||
}
|
||||
if (!matched) bfr.Add(src); // no convert; make sure to add back src, else bfr will be blank
|
||||
return matched;
|
||||
}
|
||||
public void Rebuild(Xol_convert_regy regy, byte[][] ary) {
|
||||
trie.Clear();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
byte[] key = ary[i];
|
||||
Xol_convert_grp grp = regy.Get_or_null(key); if (grp == null) continue; // vnts may not have convert mapping; EX: zh-my
|
||||
Rebuild_grp(grp);
|
||||
}
|
||||
}
|
||||
private void Rebuild_grp(Xol_convert_grp grp) {
|
||||
int len = grp.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Xol_convert_itm itm = grp.Get_at(i);
|
||||
trie.Add_obj(itm.Src(), itm.Trg()); // NOTE: for dupes, latest value wins
|
||||
}
|
||||
}
|
||||
}
|
||||
141
400_xowa/src/gplx/xowa/langs/vnts/converts/Xol_mw_parse_tst.java
Normal file
141
400_xowa/src/gplx/xowa/langs/vnts/converts/Xol_mw_parse_tst.java
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
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.converts; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
import org.junit.*;
|
||||
import gplx.langs.phps.*;
|
||||
public class Xol_mw_parse_tst {
|
||||
private Xol_mw_parse_fxt fxt = new Xol_mw_parse_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test_convert("$zh2Hant = array('a' => 'A', 'b' => 'B',);", String_.Concat_lines_nl
|
||||
( "// zh_zh-hant"
|
||||
, "app.langs.get('zh').converts.get('zh-hant').add_bulk("
|
||||
, "<:['"
|
||||
, "a|A"
|
||||
, "b|B"
|
||||
, "']:>"
|
||||
, ");"
|
||||
));
|
||||
}
|
||||
// @Test public void Run() {
|
||||
// Io_url src_dir = Io_url_.new_dir_("C:\\xowa\\bin\\any\\xowa\\lang\\mediawiki\\converts\\");
|
||||
// Io_url trg_dir = Io_url_.new_dir_("C:\\xowa\\bin\\any\\xowa\\lang\\");
|
||||
// fxt.Test_run(src_dir, trg_dir);
|
||||
// }
|
||||
}
|
||||
class Xol_mw_parse_grp {
|
||||
public byte[] Lng() {return lng;} public Xol_mw_parse_grp Lng_(byte[] v) {lng = v; return this;} private byte[] lng;
|
||||
public byte[] Vnt() {return vnt;} public Xol_mw_parse_grp Vnt_(byte[] v) {vnt = v; return this;} private byte[] vnt;
|
||||
public Xol_mw_parse_itm[] Itms() {return itms;} public Xol_mw_parse_grp Itms_(Xol_mw_parse_itm[] v) {itms = v; return this;} private Xol_mw_parse_itm[] itms;
|
||||
public void Write_as_gfs(Bry_bfr bfr) {
|
||||
int itms_len = itms.length;
|
||||
Write_bgn(bfr);
|
||||
for (int i = 0; i < itms_len; i++) {
|
||||
Xol_mw_parse_itm itm = (Xol_mw_parse_itm)itms[i];
|
||||
Write_itm(bfr, itm);
|
||||
}
|
||||
Write_end(bfr);
|
||||
}
|
||||
private void Write_bgn(Bry_bfr bfr) {
|
||||
bfr.Add_str_a7("// ").Add(lng).Add_str("_").Add(vnt).Add_byte_nl();
|
||||
bfr.Add_str_a7("app.langs.get('");
|
||||
bfr.Add(lng);
|
||||
bfr.Add_str_a7("').converts.get('");
|
||||
bfr.Add(vnt);
|
||||
bfr.Add_str_a7("').add_bulk(");
|
||||
bfr.Add_byte_nl().Add_str("<:['").Add_byte_nl();
|
||||
}
|
||||
private void Write_itm(Bry_bfr bfr, Xol_mw_parse_itm itm) {
|
||||
bfr.Add(itm.Src());
|
||||
bfr.Add_byte_pipe();
|
||||
bfr.Add(itm.Trg());
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
private void Write_end(Bry_bfr bfr) {
|
||||
bfr.Add_str_a7("']:>").Add_byte_nl();
|
||||
bfr.Add_str_a7(");").Add_byte_nl();
|
||||
}
|
||||
}
|
||||
class Xol_mw_parse_itm {
|
||||
public Xol_mw_parse_itm(byte[] src, byte[] trg) {this.src = src; this.trg = trg;}
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public byte[] Trg() {return trg;} private byte[] trg;
|
||||
}
|
||||
class Xol_mw_parse_fxt {
|
||||
public void Test_convert(String mw, String expd) {
|
||||
Xol_mw_parse_grp[] actl_ary = Parse(Bry_.new_u8(mw));
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
actl_ary[0].Write_as_gfs(bfr);
|
||||
Tfds.Eq_str_lines(expd, bfr.Xto_str());
|
||||
}
|
||||
public void Test_run(Io_url src_dir, Io_url trg_dir) {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
Io_url[] fils = Io_mgr.I.QueryDir_fils(src_dir);
|
||||
int fils_len = fils.length;
|
||||
for (int i = 0; i < fils_len; i++) {
|
||||
Io_url fil = fils[i];
|
||||
byte[] src = Io_mgr.I.LoadFilBry(fil);
|
||||
Xol_mw_parse_grp[] itms = Parse(src);
|
||||
int itms_len = itms.length;
|
||||
String lang_name = String_.Lower(String_.Mid(fil.NameOnly(), 0, 2)); // ZhConversion.php -> Zh
|
||||
for (int j = 0; j < itms_len; j++) {
|
||||
Xol_mw_parse_grp itm = itms[j];
|
||||
itm.Write_as_gfs(bfr);
|
||||
}
|
||||
Io_url trg_fil = Xol_convert_regy.Bld_url(trg_dir, lang_name);
|
||||
Io_mgr.I.SaveFilBry(trg_fil, bfr.Xto_bry_and_clear());
|
||||
}
|
||||
}
|
||||
public Xol_mw_parse_grp[] Parse(byte[] src) {
|
||||
List_adp list = List_adp_.new_();
|
||||
Php_parser parser = new Php_parser();
|
||||
Gfo_msg_log msg_log = new Gfo_msg_log("xowa");
|
||||
Php_evaluator evaluator = new Php_evaluator(msg_log);
|
||||
parser.Parse_tkns(src, evaluator);
|
||||
Php_line[] lines = (Php_line[])evaluator.List().To_ary(Php_line.class);
|
||||
int lines_len = lines.length;
|
||||
for (int i = 0; i < lines_len; i++) {
|
||||
Php_line_assign line = (Php_line_assign)lines[i];
|
||||
Xol_mw_parse_grp grp = Parse_grp(line);
|
||||
list.Add(grp);
|
||||
}
|
||||
return (Xol_mw_parse_grp[])list.To_ary(Xol_mw_parse_grp.class);
|
||||
}
|
||||
private List_adp tmp_itm_list = List_adp_.new_();
|
||||
private Xol_mw_parse_grp Parse_grp(Php_line_assign line) {
|
||||
Xol_mw_parse_grp grp = new Xol_mw_parse_grp();
|
||||
byte[] key = line.Key().Val_obj_bry(); // EX: "zh2Hant"
|
||||
key = Bry_.Lcase__all(key); // EX: "zh2hant"
|
||||
byte[][] parts = Bry_split_.Split(key, Byte_ascii.Num_2); // EX: "zh", "hant"
|
||||
byte[] src = parts[0];
|
||||
byte[] trg = Bry_.Add(parts[0], new byte[] {Byte_ascii.Dash}, parts[1]);
|
||||
grp.Lng_(src).Vnt_(trg);
|
||||
Parse_itms(line, grp);
|
||||
return grp;
|
||||
}
|
||||
private void Parse_itms(Php_line_assign line, Xol_mw_parse_grp grp) {
|
||||
Php_itm_ary ary = (Php_itm_ary)line.Val();
|
||||
tmp_itm_list.Clear();
|
||||
int subs_len = ary.Subs_len();
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Php_itm_kv kv = (Php_itm_kv)ary.Subs_get(i);
|
||||
Xol_mw_parse_itm itm = new Xol_mw_parse_itm(kv.Key().Val_obj_bry(), kv.Val().Val_obj_bry());
|
||||
tmp_itm_list.Add(itm);
|
||||
}
|
||||
grp.Itms_((Xol_mw_parse_itm[])tmp_itm_list.To_ary(Xol_mw_parse_itm.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user