mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Wiki: Support renamed folders (fix)
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
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.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
public class Xol_csv_parser {
|
||||
public void Save(Bry_bfr bfr, byte[] src) {
|
||||
int len = src.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Cr: bfr.Add_byte(Byte_ascii.Backslash); bfr.Add_byte(Byte_ascii.Ltr_r); break;
|
||||
case Byte_ascii.Nl: bfr.Add_byte(Byte_ascii.Backslash); bfr.Add_byte(Byte_ascii.Ltr_n); break;
|
||||
case Byte_ascii.Tab: bfr.Add_byte(Byte_ascii.Backslash); bfr.Add_byte(Byte_ascii.Ltr_t); break;
|
||||
case Byte_ascii.Backslash: bfr.Add_byte(Byte_ascii.Backslash); bfr.Add_byte(Byte_ascii.Backslash); break;
|
||||
case Byte_ascii.Pipe: bfr.Add(Bry_pipe); break;
|
||||
default: bfr.Add_byte(b); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public byte[] Load(byte[] src, int bgn, int end) {Load(tmp_bfr, src, bgn, end); return tmp_bfr.To_bry_and_clear();}
|
||||
public void Load(Bry_bfr bfr, byte[] src) {Load(bfr, src, 0, src.length);}
|
||||
public void Load(Bry_bfr bfr, byte[] src, int bgn, int end) {
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash: {
|
||||
int nxt_pos = i + 1; if (nxt_pos == end) throw Err_.new_wo_type("backslash cannot be last character");
|
||||
byte nxt_byte = src[nxt_pos];
|
||||
switch (nxt_byte) {
|
||||
case Byte_ascii.Backslash: bfr.Add_byte(Byte_ascii.Backslash); break;
|
||||
case Byte_ascii.Ltr_r: bfr.Add_byte(Byte_ascii.Cr); break;
|
||||
case Byte_ascii.Ltr_n: bfr.Add_byte(Byte_ascii.Nl); break;
|
||||
case Byte_ascii.Ltr_t: bfr.Add_byte(Byte_ascii.Tab); break;
|
||||
case Byte_ascii.Ltr_u:
|
||||
int utf_len = 1;
|
||||
for (int j = i + 6; j < end; j += 6) { // iterate over rest of String; EX: \u0123
|
||||
if (j + 1 < end && src[j] == Byte_ascii.Backslash && src[j + 1] == Byte_ascii.Ltr_u)
|
||||
++utf_len;
|
||||
else
|
||||
break;
|
||||
}
|
||||
byte[] utf_bytes = new byte[utf_len]; int utf_idx = 0;
|
||||
int utf_pos = i + 2;
|
||||
for (int j = 0; j < utf_len; j++) {
|
||||
int utf_int = Int_.To_int_hex(src, utf_pos, utf_pos + 4);
|
||||
if (utf_int == -1) throw Err_.new_wo_type("invalid value for \\u", "val", String_.new_u8(src, bgn, end));
|
||||
utf_bytes[utf_idx++] = (byte)utf_int;
|
||||
utf_pos += 6;
|
||||
}
|
||||
int utf_int_decoded = gplx.core.intls.Utf16_.Decode_to_int(utf_bytes, 0);
|
||||
bfr.Add(gplx.core.intls.Utf16_.Encode_int_to_bry(utf_int_decoded));
|
||||
nxt_pos = i + (utf_len * 6) - 1; // -1 b/c "for" will ++
|
||||
break;
|
||||
default:
|
||||
bfr.Add_byte(b).Add_byte(nxt_byte);
|
||||
break;
|
||||
}
|
||||
i = nxt_pos;
|
||||
break;
|
||||
}
|
||||
default: bfr.Add_byte(b); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
private static final byte[] Bry_pipe = Bry_.new_a7("\\u007C");
|
||||
private static final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
public static final Xol_csv_parser Instance = new Xol_csv_parser(); Xol_csv_parser() {}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
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.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import org.junit.*;
|
||||
public class Xol_csv_parser_tst {
|
||||
Xol_csv_parser_fxt fxt = new Xol_csv_parser_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Save() {fxt.Tst_save("a\r\n\t|d", "a\\r\\n\\t\\u007Cd");}
|
||||
@Test public void Load() {fxt.Tst_load("a\r\n\t|d", "a\\r\\n\\t\\u007Cd");}
|
||||
@Test public void Save_backslash() {fxt.Tst_save("a\\\\n", "a\\\\\\\\n");}
|
||||
@Test public void Load_backslash() {fxt.Tst_load("a\\\\n", "a\\\\\\\\n");}
|
||||
@Test public void Utf() {fxt.Tst_load(" ", "\\u00c2\\u00a0");} // NOTE: 1st String is nbsp;
|
||||
}
|
||||
class Xol_csv_parser_fxt {
|
||||
Xol_csv_parser parser = Xol_csv_parser.Instance; Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
public void Clear() {}
|
||||
public void Tst_save(String raw, String expd) {
|
||||
parser.Save(tmp_bfr, Bry_.new_u8(raw));
|
||||
Tfds.Eq(expd, tmp_bfr.To_str_and_clear());
|
||||
}
|
||||
public void Tst_load(String expd, String raw_str) {
|
||||
byte[] raw = Bry_.new_u8(raw_str);
|
||||
parser.Load(tmp_bfr, raw, 0, raw.length);
|
||||
Tfds.Eq(expd, tmp_bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
||||
@@ -1,275 +0,0 @@
|
||||
/*
|
||||
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.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import gplx.core.intls.*;
|
||||
import gplx.xowa.apps.gfs.*;
|
||||
import gplx.xowa.langs.numbers.*; import gplx.xowa.langs.msgs.*; import gplx.xowa.langs.kwds.*; import gplx.xowa.langs.bldrs.*; import gplx.xowa.langs.specials.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
public class Xol_lang_srl {
|
||||
public static Xow_ns[] Load_ns_grps(byte[] src) {
|
||||
int src_len = src.length, pos = 0, fld_bgn = 0;
|
||||
int cur_id = -1;
|
||||
List_adp rv = List_adp_.New(); Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
while (true) {
|
||||
boolean last = pos == src_len; // NOTE: logic occurs b/c of \n}~-> dlm which gobbles up last \n
|
||||
byte b = last ? Byte_ascii.Nl : src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Pipe:
|
||||
cur_id = Bry_.To_int_or(src, fld_bgn, pos, Int_.Min_value);
|
||||
if (cur_id == Int_.Min_value) throw Err_.new_wo_type("invalid_id", "id", String_.new_u8(src, fld_bgn, pos));
|
||||
fld_bgn = pos + 1;
|
||||
break;
|
||||
case Byte_ascii.Nl:
|
||||
byte[] cur_name = csv_parser.Load(src, fld_bgn, pos);
|
||||
cur_name = Xoa_ttl.Replace_spaces(cur_name); // NOTE: *.gfs files will have names with \s instead of _; this comes from Language.php which also has same \s convention; EX: "Template talk" instead of "Template_talk"
|
||||
Xow_ns ns = new Xow_ns(cur_id, Xow_ns_case_.Tid__1st, cur_name, false);
|
||||
rv.Add(ns);
|
||||
fld_bgn = pos + 1;
|
||||
cur_id = -1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (last) break;
|
||||
++pos;
|
||||
}
|
||||
return (Xow_ns[])rv.To_ary(Xow_ns.class);
|
||||
}
|
||||
public static void Load_keywords(Xol_kwd_mgr keyword_mgr, byte[] src) {
|
||||
int src_len = src.length, pos = 0, fld_bgn = 0, fld_idx = 0;
|
||||
boolean cur_cs = false; byte[] cur_key = Bry_.Empty;
|
||||
List_adp cur_words = List_adp_.New();
|
||||
Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
while (true) {
|
||||
boolean last = pos == src_len; // NOTE: logic occurs b/c of \n}~-> dlm which gobbles up last \n
|
||||
byte b = last ? Byte_ascii.Nl : src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Pipe:
|
||||
switch (fld_idx) {
|
||||
case 0:
|
||||
cur_key = csv_parser.Load(src, fld_bgn, pos);
|
||||
break;
|
||||
case 1:
|
||||
byte cs_byte = src[pos - 1];
|
||||
switch (cs_byte) {
|
||||
case Byte_ascii.Num_0: cur_cs = false; break;
|
||||
case Byte_ascii.Num_1: cur_cs = true; break;
|
||||
default: throw Err_.new_wo_type("case sensitive should be 0 or 1", "cs", Byte_.To_str(cs_byte));
|
||||
}
|
||||
break;
|
||||
}
|
||||
fld_bgn = pos + 1;
|
||||
++fld_idx;
|
||||
break;
|
||||
case Byte_ascii.Tilde:
|
||||
byte[] word = csv_parser.Load(src, fld_bgn, pos);
|
||||
cur_words.Add(word);
|
||||
fld_bgn = pos + 1;
|
||||
break;
|
||||
case Byte_ascii.Nl:
|
||||
if (cur_words.Count() > 0) { // guard against blank line wiping out entries; EX: "toc|0|toc1\n\n"; 2nd \n will get last grp and make 0 entries
|
||||
int cur_id = Xol_kwd_grp_.Id_by_bry(cur_key); if (cur_id == -1) throw Err_.new_wo_type("key does not have id", "id", cur_id);
|
||||
Xol_kwd_grp grp = keyword_mgr.Get_or_new(cur_id);
|
||||
grp.Srl_load(cur_cs, (byte[][])cur_words.To_ary(byte[].class));
|
||||
}
|
||||
fld_bgn = pos + 1;
|
||||
fld_idx = 0;
|
||||
cur_words.Clear();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (last) break;
|
||||
++pos;
|
||||
}
|
||||
// return (Xol_kwd_grp[])rv.To_ary(typeof(Xol_kwd_grp));
|
||||
}
|
||||
public static void Load_messages(Xol_msg_mgr msg_mgr, byte[] src) {
|
||||
int src_len = src.length, pos = 0, fld_bgn = 0;
|
||||
byte[] cur_key = Bry_.Empty;
|
||||
Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
while (true) {
|
||||
boolean last = pos == src_len; // NOTE: logic occurs b/c of \n}~-> dlm which gobbles up last \n
|
||||
byte b = last ? Byte_ascii.Nl : src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Pipe:
|
||||
cur_key = csv_parser.Load(src, fld_bgn, pos);
|
||||
fld_bgn = pos + 1;
|
||||
break;
|
||||
case Byte_ascii.Nl:
|
||||
byte[] cur_val = csv_parser.Load(src, fld_bgn, pos);
|
||||
Xol_msg_itm itm = msg_mgr.Itm_by_key_or_new(cur_key).Defined_in_(Xol_msg_itm.Defined_in__lang); // NOTE: this proc should only be called when loading lang.gfs
|
||||
Xol_msg_itm_.update_val_(itm, cur_val);
|
||||
itm.Dirty_(true);
|
||||
fld_bgn = pos + 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (last) break;
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
public static void Load_specials(Xol_specials_mgr special_mgr, byte[] src) {
|
||||
int src_len = src.length, pos = 0, fld_bgn = 0;
|
||||
byte[] cur_key = Bry_.Empty;
|
||||
Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
while (true) {
|
||||
boolean last = pos == src_len; // NOTE: logic occurs b/c of \n}~-> dlm which gobbles up last \n
|
||||
byte b = last ? Byte_ascii.Nl : src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Pipe:
|
||||
cur_key = csv_parser.Load(src, fld_bgn, pos);
|
||||
fld_bgn = pos + 1;
|
||||
break;
|
||||
case Byte_ascii.Nl:
|
||||
byte[] cur_val_raw = csv_parser.Load(src, fld_bgn, pos);
|
||||
byte[][] cur_vals = Bry_split_.Split(cur_val_raw, Byte_ascii.Tilde);
|
||||
special_mgr.Add(cur_key, cur_vals);
|
||||
fld_bgn = pos + 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (last) break;
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
public static void Save_num_mgr(Xoa_gfs_bldr bldr, Xol_num_mgr num_mgr) {
|
||||
Xol_transform_mgr separators_mgr = num_mgr.Separators_mgr(); int separators_len = separators_mgr.Len();
|
||||
Xol_transform_mgr digits_mgr = num_mgr.Digits_mgr(); int digits_len = digits_mgr.Len();
|
||||
byte[] digit_grouping_pattern = num_mgr.Num_grp_fmtr().Digit_grouping_pattern();
|
||||
if (separators_len > 0 || digits_len > 0 || digit_grouping_pattern != null) {
|
||||
bldr.Add_proc_init_one(Xol_lang_itm.Invk_numbers).Add_curly_bgn_nl(); // numbers {
|
||||
if (digit_grouping_pattern != null) {
|
||||
bldr.Add_indent(1).Add_eq_str(Xol_num_mgr.Invk_digit_grouping_pattern, digit_grouping_pattern);
|
||||
}
|
||||
if (separators_len > 0) {
|
||||
bldr.Add_indent(1).Add_proc_init_one(Xol_num_mgr.Invk_separators).Add_curly_bgn_nl(); // separators {
|
||||
bldr.Add_indent(2).Add_proc_init_one(Xol_num_mgr.Invk_clear).Add_term_nl(); // clear;
|
||||
for (int i = 0; i < separators_len; i++) {
|
||||
Keyval kv = separators_mgr.Get_at(i);
|
||||
String k = kv.Key(), v = kv.Val_to_str_or_empty();
|
||||
bldr.Add_indent(2).Add_proc_init_many(Xol_transform_mgr.Invk_set).Add_parens_str_many(k, v).Add_term_nl(); // set('k', 'v');
|
||||
}
|
||||
bldr.Add_indent(1).Add_curly_end_nl(); // }
|
||||
}
|
||||
if (digits_len > 0) {
|
||||
bldr.Add_indent(1).Add_proc_init_one(Xol_num_mgr.Invk_digits).Add_curly_bgn_nl(); // digits {
|
||||
bldr.Add_indent(2).Add_proc_init_one(Xol_num_mgr.Invk_clear).Add_term_nl(); // clear;
|
||||
for (int i = 0; i < digits_len; i++) {
|
||||
Keyval kv = digits_mgr.Get_at(i);
|
||||
String k = kv.Key(), v = kv.Val_to_str_or_empty();
|
||||
bldr.Add_indent(2).Add_proc_init_many(Xol_transform_mgr.Invk_set).Add_parens_str_many(k, v).Add_term_nl(); // set('k', 'v');
|
||||
}
|
||||
bldr.Add_indent(1).Add_curly_end_nl(); // }
|
||||
}
|
||||
bldr.Add_curly_end_nl(); // }
|
||||
}
|
||||
}
|
||||
public static void Save_ns_grps(Xoa_gfs_bldr bldr, Xol_ns_grp ns_grp, String proc_invk) {
|
||||
int ns_grp_len = ns_grp.Len(); Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
if (ns_grp_len == 0) return;
|
||||
Bry_bfr bfr = bldr.Bfr();
|
||||
bldr.Add_proc_cont_one(proc_invk).Add_nl();
|
||||
bldr.Add_indent().Add_proc_cont_one(Invk_load_text).Add_paren_bgn().Add_nl(); // .load_text(\n
|
||||
bldr.Add_quote_xtn_bgn(); // <~{\n'
|
||||
for (int i = 0; i < ns_grp_len; i++) {
|
||||
Xow_ns ns = ns_grp.Get_at(i);
|
||||
bfr.Add_int_variable(ns.Id()).Add_byte_pipe(); // id|
|
||||
csv_parser.Save(bfr, ns.Name_db()); // name
|
||||
bfr.Add_byte_nl(); // \n
|
||||
}
|
||||
bldr.Add_quote_xtn_end(); // ']:>\n
|
||||
bldr.Add_paren_end().Add_proc_cont_one(Invk_lang).Add_nl(); // ).lang\n
|
||||
}
|
||||
public static void Save_specials(Xoa_gfs_bldr bldr, Xol_specials_mgr specials_mgr) {
|
||||
int specials_len = specials_mgr.Len(); Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
if (specials_len == 0) return;
|
||||
Bry_bfr bfr = bldr.Bfr();
|
||||
bldr.Add_proc_cont_one(Xol_lang_itm.Invk_specials).Add_nl();
|
||||
bldr.Add_indent().Add_proc_cont_one(Xol_specials_mgr.Invk_clear).Add_nl();
|
||||
bldr.Add_indent().Add_proc_cont_one(Invk_load_text).Add_paren_bgn().Add_nl(); // .load_text(\n
|
||||
bldr.Add_quote_xtn_bgn(); // <~{\n'
|
||||
for (int i = 0; i < specials_len; i++) {
|
||||
Xol_specials_itm itm = specials_mgr.Get_at(i);
|
||||
bfr.Add(itm.Special()).Add_byte_pipe(); // id|
|
||||
int aliases_len = itm.Aliases().length;
|
||||
for (int j = 0; j < aliases_len; j++) {
|
||||
if (j != 0) bfr.Add_byte(Byte_ascii.Tilde);
|
||||
csv_parser.Save(bfr, itm.Aliases()[j]); // name
|
||||
}
|
||||
bfr.Add_byte_nl(); // \n
|
||||
}
|
||||
bldr.Add_quote_xtn_end(); // ']:>\n
|
||||
bldr.Add_paren_end().Add_proc_cont_one(Invk_lang).Add_nl(); // ).lang\n
|
||||
}
|
||||
public static void Save_keywords(Xoa_gfs_bldr bldr, Xol_kwd_mgr kwd_mgr) {
|
||||
int len = kwd_mgr.Len(); Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_kwd_grp grp = kwd_mgr.Get_at(i); if (grp == null) continue; // some items may not be loaded/set by lang
|
||||
++count;
|
||||
}
|
||||
if (count == 0) return;
|
||||
Bry_bfr bfr = bldr.Bfr();
|
||||
bldr.Add_proc_cont_one(Xol_lang_itm.Invk_keywords).Add_nl(); // .keywords\n
|
||||
bldr.Add_indent().Add_proc_cont_one(Invk_load_text).Add_paren_bgn().Add_nl(); // .load_text(\n
|
||||
bldr.Add_quote_xtn_bgn(); // <~{\n'
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_kwd_grp grp = kwd_mgr.Get_at(i); if (grp == null) continue; // some items may not be loaded/set by lang
|
||||
csv_parser.Save(bfr, grp.Key()); // key
|
||||
bfr.Add_byte_pipe(); // |
|
||||
bfr.Add_int_bool(grp.Case_match()).Add_byte_pipe(); // 1|
|
||||
int word_len = grp.Itms().length;
|
||||
for (int j = 0; j < word_len; j++) {
|
||||
Xol_kwd_itm word = grp.Itms()[j];
|
||||
csv_parser.Save(bfr, word.Val()); // word
|
||||
bfr.Add_byte(Byte_ascii.Tilde); // ~
|
||||
}
|
||||
bldr.Add_nl(); // \n
|
||||
}
|
||||
bldr.Add_quote_xtn_end(); // ']:>\n
|
||||
bldr.Add_paren_end().Add_proc_cont_one(Invk_lang).Add_nl(); // ).lang\n
|
||||
}
|
||||
public static void Save_messages(Xoa_gfs_bldr bldr, Xol_msg_mgr msg_mgr, boolean dirty) {
|
||||
int len = msg_mgr.Itms_max(); Xol_csv_parser csv_parser = Xol_csv_parser.Instance;
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_msg_itm itm = msg_mgr.Itm_by_id_or_null(i); if (itm == null) continue; // some items may not be loaded/set by lang
|
||||
if (dirty && !itm.Dirty()) continue; // TEST:
|
||||
++count;
|
||||
}
|
||||
if (count == 0) return;
|
||||
Bry_bfr bfr = bldr.Bfr();
|
||||
bldr.Add_proc_cont_one(Xol_lang_itm.Invk_messages).Add_nl(); // .keywords\n
|
||||
bldr.Add_indent().Add_proc_cont_one(Invk_load_text).Add_paren_bgn().Add_nl(); // .load_text(\n
|
||||
bldr.Add_quote_xtn_bgn(); // <~{\n'
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_msg_itm itm = msg_mgr.Itm_by_id_or_null(i); if (itm == null) continue; // some items may not be loaded/set by lang
|
||||
if (dirty && !itm.Dirty()) continue; // TEST:
|
||||
csv_parser.Save(bfr, itm.Key()); // key
|
||||
bfr.Add_byte_pipe(); // |
|
||||
csv_parser.Save(bfr, itm.Val()); // val
|
||||
bfr.Add_byte_nl(); // \n
|
||||
}
|
||||
bldr.Add_quote_xtn_end(); // ']:>\n
|
||||
bldr.Add_paren_end().Add_proc_cont_one(Invk_lang).Add_nl(); // ).lang\n
|
||||
}
|
||||
public static final String Invk_load_text = "load_text", Invk_lang = "lang";
|
||||
}
|
||||
@@ -1,348 +0,0 @@
|
||||
/*
|
||||
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.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import org.junit.*; import gplx.core.strings.*;
|
||||
import gplx.core.intls.*;
|
||||
import gplx.xowa.apps.gfs.*;
|
||||
import gplx.xowa.langs.numbers.*; import gplx.xowa.langs.msgs.*; import gplx.xowa.langs.kwds.*; import gplx.xowa.langs.bldrs.*; import gplx.xowa.langs.specials.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
public class Xol_lang_srl_tst {
|
||||
private Xol_lang_srl_fxt fxt = new Xol_lang_srl_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Ns_names() {
|
||||
String raw = String_.Concat_lines_nl
|
||||
( "ns_names"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "6|Filex"
|
||||
, "10|Templatex"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_ns_grp grp = fxt.Lang().Ns_names();
|
||||
fxt.Tst_ns_grp(grp, fxt.ns_(6, "Filex"), fxt.ns_(10, "Templatex"));
|
||||
fxt.Run_save_ns_grp(grp, Xol_lang_itm.Invk_ns_names, raw);
|
||||
}
|
||||
@Test public void Ns_aliases() {
|
||||
String raw = String_.Concat_lines_nl
|
||||
( "ns_aliases"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "6|Filex"
|
||||
, "10|Templatex"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_ns_grp grp = fxt.Lang().Ns_aliases();
|
||||
fxt.Tst_ns_grp(grp, fxt.ns_(6, "Filex"), fxt.ns_(10, "Templatex"));
|
||||
fxt.Run_save_ns_grp(grp, Xol_lang_itm.Invk_ns_aliases, raw);
|
||||
}
|
||||
@Test public void Kwds() {
|
||||
String raw = String_.Concat_lines_nl // NOTE: notoc must go before toc because ID order
|
||||
( "keywords"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "notoc|1|no_table_of_contents~toc_n~"
|
||||
, "toc|0|table_of_contents~toc_y~"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_kwd_mgr kwd_mgr = fxt.Lang().Kwd_mgr();
|
||||
fxt.Tst_keywords(kwd_mgr, fxt.kwd_("notoc", true, "no_table_of_contents", "toc_n"), fxt.kwd_("toc", false, "table_of_contents", "toc_y"));
|
||||
fxt.Run_save_kwd_mgr(kwd_mgr, Xol_lang_itm.Invk_keywords, raw);
|
||||
}
|
||||
@Test public void Specials() {
|
||||
String raw = String_.Concat_lines_nl // NOTE: notoc must go before toc because ID order
|
||||
( "specials"
|
||||
, " .clear"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "Randompage|PageAuHasard~Page_au_hasard"
|
||||
, "Search|Recherche~Rechercher"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_specials_mgr specials_mgr = fxt.Lang().Specials_mgr();
|
||||
fxt.Tst_specials(specials_mgr, fxt.special_("Randompage", "PageAuHasard", "Page_au_hasard"), fxt.special_("Search", "Recherche", "Rechercher"));
|
||||
fxt.Run_save_specials_mgr(specials_mgr, Xol_lang_itm.Invk_specials, raw);
|
||||
}
|
||||
@Test public void Kwds_blank_line() { // PURPOSE.fix: extra blank line negates entire entry
|
||||
String raw = String_.Concat_lines_nl
|
||||
( "keywords"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "toc|0|table_of_contents~toc_y~"
|
||||
, ""
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_kwd_mgr kwd_mgr = fxt.Lang().Kwd_mgr();
|
||||
fxt.Tst_keywords(kwd_mgr, fxt.kwd_("toc", false, "table_of_contents", "toc_y")); // make sure 2 items (and not 0)
|
||||
}
|
||||
@Test public void Msgs() {
|
||||
String raw = String_.Concat_lines_nl
|
||||
( "messages"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "sunday|dimanche"
|
||||
, "monday|lundi"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_msg_mgr msg_mgr = fxt.Lang().Msg_mgr();
|
||||
fxt.Tst_messages(msg_mgr, fxt.msg_("sunday", "dimanche"), fxt.msg_("monday", "lundi"));
|
||||
fxt.Run_save_msg_mgr(msg_mgr, Xol_lang_itm.Invk_messages, raw);
|
||||
}
|
||||
@Test public void Fallback() {
|
||||
Io_mgr.Instance.SaveFilStr(Xol_lang_itm_.xo_lang_fil_(fxt.App().Fsys_mgr(), "zh-hans"), String_.Concat_lines_nl
|
||||
( "this"
|
||||
, ".keywords"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "toc|0|table_of_contents~toc_y~"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
, ".ns_names"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "6|FileA"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
, ".messages"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "sunday|sunday1"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
, ";"
|
||||
));
|
||||
String raw = String_.Concat_lines_nl // NOTE: notoc must go before toc because ID order
|
||||
( "fallback_load('zh-hans')"
|
||||
, ".keywords"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "notoc|1|no_table_of_contents~toc_n~"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
, ".ns_names"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "6|FileB"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
, ".messages"
|
||||
, " .load_text("
|
||||
, "<:['"
|
||||
, "monday|monday1"
|
||||
, "']:>"
|
||||
, ").lang"
|
||||
);
|
||||
fxt.Invk(raw);
|
||||
Xol_kwd_mgr kwd_mgr = fxt.Lang().Kwd_mgr();
|
||||
fxt.Tst_keywords(kwd_mgr, fxt.kwd_("notoc", true, "no_table_of_contents", "toc_n"), fxt.kwd_("toc", false, "table_of_contents", "toc_y"));
|
||||
fxt.Tst_ns_grp(fxt.Lang().Ns_names(), fxt.ns_(6, "FileA"), fxt.ns_(6, "FileB"));
|
||||
fxt.Tst_messages(fxt.Lang().Msg_mgr(), fxt.msg_("sunday", "sunday1"), fxt.msg_("monday", "monday1"));
|
||||
}
|
||||
@Test public void Fallback_circular() { // PURPOSE: pt and pt-br cite each other as fallback in Messages*.php; DATE:2013-02-18
|
||||
Io_mgr.Instance.SaveFilStr(Xol_lang_itm_.xo_lang_fil_(fxt.App().Fsys_mgr(), "pt") , "fallback_load('pt-br');");
|
||||
Io_mgr.Instance.SaveFilStr(Xol_lang_itm_.xo_lang_fil_(fxt.App().Fsys_mgr(), "pt-br") , "fallback_load('pt');");
|
||||
Xol_lang_itm lang = new Xol_lang_itm(fxt.App().Lang_mgr(), Bry_.new_a7("pt"));
|
||||
lang.Init_by_load();
|
||||
}
|
||||
@Test public void Num_fmt() {
|
||||
String raw = String_.Concat_lines_nl
|
||||
( "numbers {"
|
||||
, " separators {"
|
||||
, " clear;"
|
||||
, " set(',', '.');"
|
||||
, " set('.', ',');"
|
||||
, " }"
|
||||
, "}"
|
||||
);
|
||||
fxt.Invk_no_semic(raw);
|
||||
fxt.Tst_num_fmt("1234,56", "1.234.56"); // NOTE: dot is repeated; confirmed with dewiki and {{formatnum:1234,56}}
|
||||
fxt.Run_save_num_mgr(fxt.Lang().Num_mgr(), raw);
|
||||
}
|
||||
@Test public void Num_fmt_apos() { // PURPOSE:de.ch has apos which breaks gfs
|
||||
fxt .Init_clear()
|
||||
.Init_separators(",", "'")
|
||||
.Init_separators(".", ",")
|
||||
;
|
||||
fxt.Run_save_num_mgr(fxt.Lang().Num_mgr(), String_.Concat_lines_nl
|
||||
( "numbers {"
|
||||
, " separators {"
|
||||
, " clear;"
|
||||
, " set(',', '''');"
|
||||
, " set('.', ',');"
|
||||
, " }"
|
||||
, "}"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Xol_lang_srl_fxt {
|
||||
public void Clear() {
|
||||
app = Xoa_app_fxt.Make__app__edit();
|
||||
lang = new Xol_lang_itm(app.Lang_mgr(), Bry_.new_a7("fr"));
|
||||
Xoa_gfs_mgr.Msg_parser_init(); // required by fallback_load
|
||||
} GfsCtx ctx = GfsCtx.new_(); Xoa_gfs_bldr bldr = new Xoa_gfs_bldr(); //Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
public Xoae_app App() {return app;} private Xoae_app app;
|
||||
public Xol_lang_itm Lang() {return lang;} private Xol_lang_itm lang;
|
||||
public Xow_ns ns_(int id, String s) {return new Xow_ns(id, Xow_ns_case_.Tid__1st, Bry_.new_u8(s), false);}
|
||||
public Xol_specials_itm special_(String key, String... words) {return new Xol_specials_itm(Bry_.new_u8(key), Bry_.Ary(words));}
|
||||
public Xol_kwd_grp kwd_(String key, boolean case_match, String... words) {
|
||||
Xol_kwd_grp rv = new Xol_kwd_grp(Bry_.new_u8(key));
|
||||
rv.Srl_load(case_match, Bry_.Ary(words));
|
||||
return rv;
|
||||
}
|
||||
public Xol_msg_itm msg_(String key, String val) {
|
||||
Xol_msg_itm rv = lang.Msg_mgr().Itm_by_key_or_new(Bry_.new_u8(key));
|
||||
rv.Atrs_set(Bry_.new_u8(val), false, false);
|
||||
return rv;
|
||||
}
|
||||
public Xol_lang_srl_fxt Init_clear() {
|
||||
lang.Num_mgr().Clear();
|
||||
return this;
|
||||
}
|
||||
public Xol_lang_srl_fxt Init_separators(String k, String v) {
|
||||
lang.Num_mgr().Separators_mgr().Set(Bry_.new_u8(k), Bry_.new_u8(v));
|
||||
return this;
|
||||
}
|
||||
public void Invk(String raw) {
|
||||
app.Gfs_mgr().Run_str_for(lang, raw + ";");
|
||||
}
|
||||
public void Invk_no_semic(String raw) {
|
||||
app.Gfs_mgr().Run_str_for(lang, raw);
|
||||
}
|
||||
public void Tst_ns_grp(Xol_ns_grp grp, Xow_ns... expd_ns) {
|
||||
Tfds.Eq_str_lines(Xto_str(expd_ns), Xto_str(To_ary(grp)));
|
||||
}
|
||||
public void Run_save_ns_grp(Xol_ns_grp grp, String invk, String raw) {
|
||||
Xol_lang_srl.Save_ns_grps(bldr, grp, invk);
|
||||
Tfds.Eq_str_lines("." + raw, bldr.Bfr().To_str_and_clear());
|
||||
}
|
||||
public void Run_save_kwd_mgr(Xol_kwd_mgr kwd_mgr, String invk, String raw) {
|
||||
Xol_lang_srl.Save_keywords(bldr, kwd_mgr);
|
||||
Tfds.Eq_str_lines("." + raw, bldr.Bfr().To_str_and_clear());
|
||||
}
|
||||
public void Run_save_msg_mgr(Xol_msg_mgr msg_mgr, String invk, String raw) {
|
||||
Xol_lang_srl.Save_messages(bldr, msg_mgr, true);
|
||||
Tfds.Eq_str_lines("." + raw, bldr.Bfr().To_str_and_clear());
|
||||
}
|
||||
public void Run_save_num_mgr(Xol_num_mgr num_mgr, String raw) {
|
||||
Xol_lang_srl.Save_num_mgr(bldr, num_mgr);
|
||||
Tfds.Eq_str_lines(raw, bldr.Bfr().To_str_and_clear());
|
||||
}
|
||||
public void Run_save_specials_mgr(Xol_specials_mgr specials_mgr, String invk, String raw) {
|
||||
Xol_lang_srl.Save_specials(bldr, specials_mgr);
|
||||
Tfds.Eq_str_lines("." + raw, bldr.Bfr().To_str_and_clear());
|
||||
}
|
||||
public void Tst_num_fmt(String raw, String expd) {Tfds.Eq(expd, String_.new_u8(lang.Num_mgr().Format_num(Bry_.new_u8(raw))));}
|
||||
public void Tst_keywords(Xol_kwd_mgr kwd_mgr, Xol_kwd_grp... ary) {
|
||||
Tfds.Eq_str_lines(Xto_str(ary), Xto_str(To_ary(kwd_mgr)));
|
||||
}
|
||||
public void Tst_messages(Xol_msg_mgr msg_mgr, Xol_msg_itm... ary) {
|
||||
Tfds.Eq_str_lines(Xto_str(ary), Xto_str(To_ary(msg_mgr)));
|
||||
}
|
||||
public void Tst_specials(Xol_specials_mgr specials_mgr, Xol_specials_itm... expd) {
|
||||
Tfds.Eq_str_lines(Xto_str(expd), Xto_str(To_ary(specials_mgr)));
|
||||
}
|
||||
private String Xto_str(Xol_specials_itm[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_specials_itm itm = ary[i];
|
||||
sb.Add(itm.Special()).Add("|");
|
||||
int aliases_len = itm.Aliases().length;
|
||||
for (int j = 0; j < aliases_len; j++) {
|
||||
if (j != 0) sb.Add("~");
|
||||
sb.Add(itm.Aliases()[j]).Add_char_nl();
|
||||
}
|
||||
}
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
private Xol_specials_itm[] To_ary(Xol_specials_mgr specials_mgr) {
|
||||
int len = specials_mgr.Len();
|
||||
Xol_specials_itm[] rv = new Xol_specials_itm[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = specials_mgr.Get_at(i);
|
||||
return rv;
|
||||
}
|
||||
String Xto_str(Xow_ns[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xow_ns ns = ary[i];
|
||||
sb.Add(ns.Id()).Add("|").Add(ns.Name_db_str()).Add_char_nl();
|
||||
}
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
Xow_ns[] To_ary(Xol_ns_grp ary) {
|
||||
int len = ary.Len();
|
||||
Xow_ns[] rv = new Xow_ns[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv[i] = ary.Get_at(i);
|
||||
return rv;
|
||||
}
|
||||
Xol_kwd_grp[] To_ary(Xol_kwd_mgr kwd_mgr) {
|
||||
int len = kwd_mgr.Len();
|
||||
List_adp rv = List_adp_.New();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_kwd_grp kwd_grp = kwd_mgr.Get_at(i);
|
||||
if (kwd_grp == null) continue;
|
||||
rv.Add(kwd_grp);
|
||||
}
|
||||
return (Xol_kwd_grp[])rv.To_ary(Xol_kwd_grp.class);
|
||||
}
|
||||
String Xto_str(Xol_kwd_grp[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_kwd_grp grp = ary[i];
|
||||
sb.Add(grp.Key()).Add("|").Add(grp.Case_match() ? "1" : "0").Add("|");
|
||||
Xol_kwd_itm[] itms = grp.Itms();
|
||||
int itms_len = itms.length;
|
||||
for (int j = 0; j < itms_len; j++) {
|
||||
sb.Add(itms[i].Val()).Add(";");
|
||||
}
|
||||
sb.Add_char_nl();
|
||||
}
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
Xol_msg_itm[] To_ary(Xol_msg_mgr msg_mgr) {
|
||||
int len = msg_mgr.Itms_max();
|
||||
List_adp rv = List_adp_.New();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_msg_itm itm = msg_mgr.Itm_by_id_or_null(i);
|
||||
if (itm == null || !itm.Dirty()) continue;
|
||||
rv.Add(itm);
|
||||
}
|
||||
return (Xol_msg_itm[])rv.To_ary(Xol_msg_itm.class);
|
||||
}
|
||||
String Xto_str(Xol_msg_itm[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_msg_itm itm = ary[i];
|
||||
sb.Add(itm.Key()).Add("|").Add(itm.Val()).Add_char_nl();
|
||||
}
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
private static String_bldr sb = String_bldr_.new_();
|
||||
}
|
||||
Reference in New Issue
Block a user