mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
JsonConfig: Add Localize function for tabular data
This commit is contained in:
@@ -30,8 +30,8 @@ public class Xoa_app_ {
|
||||
}
|
||||
}
|
||||
public static final String Name = "xowa";
|
||||
public static final int Version_id = 540;
|
||||
public static final String Version = "4.5.18.1711";
|
||||
public static final int Version_id = 542;
|
||||
public static final String Version = "4.5.20.1801";
|
||||
public static String Build_date = "2012-12-30 00:00:00";
|
||||
public static String Build_date_fmt = "yyyy-MM-dd HH:mm:ss";
|
||||
public static String Op_sys_str;
|
||||
|
||||
@@ -18,7 +18,7 @@ import gplx.core.btries.*;
|
||||
import gplx.langs.htmls.*; import gplx.xowa.langs.kwds.*; import gplx.langs.htmls.entitys.*;
|
||||
import gplx.xowa.htmls.core.wkrs.hdrs.*; import gplx.xowa.htmls.core.wkrs.lnkes.*;
|
||||
import gplx.xowa.wikis.domains.*;
|
||||
import gplx.xowa.parsers.*; import gplx.xowa.parsers.apos.*; import gplx.xowa.parsers.amps.*; import gplx.xowa.parsers.lnkes.*; import gplx.xowa.parsers.lists.*; import gplx.xowa.htmls.core.wkrs.lnkis.htmls.*; import gplx.xowa.parsers.tblws.*; import gplx.xowa.parsers.paras.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.lnkis.*; import gplx.xowa.parsers.miscs.*; import gplx.xowa.parsers.htmls.*;
|
||||
import gplx.xowa.parsers.*; import gplx.xowa.parsers.apos.*; import gplx.xowa.parsers.amps.*; import gplx.xowa.parsers.lnkes.*; import gplx.xowa.parsers.lists.*; import gplx.xowa.htmls.core.wkrs.lnkis.htmls.*; import gplx.xowa.parsers.tblws.*; import gplx.xowa.parsers.paras.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.lnkis.*; import gplx.xowa.parsers.miscs.*; import gplx.xowa.parsers.htmls.*; import gplx.xowa.parsers.uniqs.*;
|
||||
import gplx.xowa.xtns.*; import gplx.xowa.xtns.cites.*; import gplx.xowa.parsers.hdrs.*;
|
||||
public class Xoh_html_wtr {
|
||||
private final Xoae_app app; private final Xowe_wiki wiki; private final Xow_html_mgr html_mgr; private final Xop_xatr_whitelist_mgr whitelist_mgr;
|
||||
@@ -90,6 +90,7 @@ public class Xoh_html_wtr {
|
||||
case Xop_tkn_itm_.Tid_tblw_tc: Tblw (bfr, ctx, hctx, src, (Xop_tblw_tkn)tkn, Gfh_tag_.Caption_lhs_bgn , Gfh_tag_.Caption_rhs, false); break;
|
||||
case Xop_tkn_itm_.Tid_newLine: New_line (bfr, ctx, hctx, src, (Xop_nl_tkn)tkn); break;
|
||||
case Xop_tkn_itm_.Tid_bry: Bry (bfr, ctx, hctx, src, (Xop_bry_tkn)tkn); break;
|
||||
case Xop_tkn_itm_.Tid_uniq: Uniq (bfr, ctx, hctx, src, (Xop_uniq_tkn)tkn); break;
|
||||
case Xop_tkn_itm_.Tid_lnki: lnki_wtr.Write_lnki(bfr, hctx, src, (Xop_lnki_tkn)tkn); break;
|
||||
case Xop_tkn_itm_.Tid_lnke: wkr__lnke.Write_html(bfr, html_mgr, this, hctx, ctx, src, (Xop_lnke_tkn)tkn); break;
|
||||
case Xop_tkn_itm_.Tid_hdr: wkr__hdr.Write_html(bfr, this, wiki, page, ctx, hctx, cfg, grp, sub_idx, src, (Xop_hdr_tkn)tkn); break;
|
||||
@@ -232,6 +233,10 @@ public class Xoh_html_wtr {
|
||||
private void Bry(Bry_bfr bfr, Xop_ctx ctx, Xoh_wtr_ctx hctx, byte[] src, Xop_bry_tkn bry) {
|
||||
bfr.Add(bry.Val());
|
||||
}
|
||||
private void Uniq(Bry_bfr bfr, Xop_ctx ctx, Xoh_wtr_ctx hctx, byte[] src, Xop_uniq_tkn tkn) {
|
||||
byte[] val = wiki.Parser_mgr().Uniq_mgr().Get(tkn.Key());
|
||||
Xoh_html_wtr_escaper.Escape(app.Parser_amp_mgr(), bfr, val, 0, val.length, true, false);
|
||||
}
|
||||
private void Under(Bry_bfr bfr, Xop_ctx ctx, Xoh_wtr_ctx hctx, byte[] src, Xop_under_tkn under) {
|
||||
if (hctx.Mode_is_alt()) return;
|
||||
switch (under.Under_tid()) {
|
||||
@@ -436,7 +441,7 @@ public class Xoh_html_wtr {
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
Mwh_atr_itm atr = ary[i];
|
||||
if (atr.Invalid()) continue;
|
||||
if (!whitelist_mgr.Chk(tag_id, src, atr)) continue;
|
||||
if (!whitelist_mgr.Chk(tag_id, atr)) continue;
|
||||
Xnde_atr_write(bfr, app, hctx, src, atr);
|
||||
}
|
||||
}
|
||||
@@ -463,7 +468,7 @@ public class Xoh_html_wtr {
|
||||
}
|
||||
else {
|
||||
if (atr.Val_bry() == null)
|
||||
bfr.Add_mid(src, atr.Val_bgn(), atr.Val_end());
|
||||
bfr.Add_mid(atr.Src(), atr.Val_bgn(), atr.Val_end());
|
||||
else
|
||||
bfr.Add(atr.Val_bry());
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Xoh_hdr_html {
|
||||
|
||||
// register hdr with TOC
|
||||
byte[] hdr_text_bry = Bld_hdr_html(hdr_text_bfr, wtr, page, ctx, hctx, src, hdr);
|
||||
hdr_text_bry = wiki.Parser_mgr().Uniq_mgr().Convert(hdr_text_bry); // need for math; DATE:2016-12-09
|
||||
hdr_text_bry = wiki.Parser_mgr().Uniq_mgr().Parse(hdr_text_bry); // need for math; DATE:2016-12-09
|
||||
Xoh_toc_itm toc_itm = hdr_is_valid && hdr_text_bry.length > 0
|
||||
? page.Html_data().Toc_mgr().Add(hdr_num, hdr_text_bry)
|
||||
: invalid_toc_itm;
|
||||
|
||||
@@ -59,9 +59,13 @@ public class Xol_lang_itm implements Gfo_invk {
|
||||
public Xol_lang_itm Fallback_bry_(byte[] v) {
|
||||
fallback_bry = v;
|
||||
fallback_bry_ary = Fallbacy_bry_ary__bld(v);
|
||||
for (byte[] key : fallback_bry_ary) {
|
||||
fallback_hash.Add_as_key_and_val(String_.new_u8(key));
|
||||
}
|
||||
return this;
|
||||
} private byte[] fallback_bry;
|
||||
public byte[][] Fallback_bry_ary() {return fallback_bry_ary;} private byte[][] fallback_bry_ary = Bry_.Ary_empty;
|
||||
public Ordered_hash Fallback_hash() {return fallback_hash;} private final Ordered_hash fallback_hash = Ordered_hash_.New();
|
||||
public boolean Dir_ltr() {return dir_ltr;} private boolean dir_ltr = true;
|
||||
public void Dir_ltr_(boolean v) {
|
||||
dir_ltr = v;
|
||||
|
||||
85
400_xowa/src/gplx/xowa/mediawiki/XophpStringTest.java
Normal file
85
400_xowa/src/gplx/xowa/mediawiki/XophpStringTest.java
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
||||
import org.junit.*; import gplx.core.tests.*; import gplx.core.btries.*;
|
||||
public class XophpStringTest {
|
||||
private final XophpStringFxt fxt = new XophpStringFxt();
|
||||
@Test public void Strspn_fwd__byte() {
|
||||
fxt.Test_strspn_fwd__byte("aaaaab", Byte_ascii.Ltr_a, 0, -1, 5); // basic
|
||||
fxt.Test_strspn_fwd__byte("aaaaab", Byte_ascii.Ltr_a, 1, -1, 4); // bgn
|
||||
fxt.Test_strspn_fwd__byte("aaaaab", Byte_ascii.Ltr_a, 1, 2, 2); // max
|
||||
}
|
||||
@Test public void Strspn_fwd__space_or_tab() {
|
||||
fxt.Test_strspn_fwd__space_or_tab(" a", 0, -1, 5); // basic
|
||||
fxt.Test_strspn_fwd__space_or_tab(" a", 1, -1, 4); // bgn
|
||||
fxt.Test_strspn_fwd__space_or_tab(" a", 1, 2, 2); // max
|
||||
}
|
||||
@Test public void Strspn_bwd__byte() {
|
||||
fxt.Test_strspn_bwd__byte("aaaaab", Byte_ascii.Ltr_a, 5, -1, 5); // basic
|
||||
fxt.Test_strspn_bwd__byte("aaaaab", Byte_ascii.Ltr_a, 4, -1, 4); // bgn
|
||||
fxt.Test_strspn_bwd__byte("aaaaab", Byte_ascii.Ltr_a, 4, 2, 2); // max
|
||||
}
|
||||
@Test public void Strspn_bwd__space_or_tab() {
|
||||
fxt.Test_strspn_bwd__space_or_tab(" a", 5, -1, 5); // basic
|
||||
fxt.Test_strspn_bwd__space_or_tab(" a", 4, -1, 4); // bgn
|
||||
fxt.Test_strspn_bwd__space_or_tab(" a", 4, 2, 2); // max
|
||||
}
|
||||
@Test public void Substr__bgn_is_neg() {
|
||||
fxt.Test_substr("abcde" , -1, "e");
|
||||
fxt.Test_substr("abcde" , -3, -1, "cd");
|
||||
}
|
||||
@Test public void Strtr() {
|
||||
fxt.Init_strtr_by_trie("01", "89", "02", "79");
|
||||
fxt.Test_strtr_by_trie("abc" , "abc"); // found=none
|
||||
fxt.Test_strtr_by_trie("ab_01_cd" , "ab_89_cd"); // found=one
|
||||
fxt.Test_strtr_by_trie("ab_01_cd_02_ef", "ab_89_cd_79_ef"); // found=many
|
||||
fxt.Test_strtr_by_trie("01_ab" , "89_ab"); // BOS
|
||||
fxt.Test_strtr_by_trie("ab_01" , "ab_89"); // EOS
|
||||
}
|
||||
}
|
||||
class XophpStringFxt {
|
||||
public void Test_strspn_fwd__byte(String src_str, byte find, int bgn, int max, int expd) {
|
||||
byte[] src_bry = Bry_.new_u8(src_str);
|
||||
Gftest.Eq__int(expd, XophpString.strspn_fwd__byte(src_bry, find, bgn, max, src_bry.length));
|
||||
}
|
||||
public void Test_strspn_fwd__space_or_tab(String src_str, int bgn, int max, int expd) {
|
||||
byte[] src_bry = Bry_.new_u8(src_str);
|
||||
Gftest.Eq__int(expd, XophpString.strspn_fwd__space_or_tab(src_bry, bgn, max, src_bry.length));
|
||||
}
|
||||
public void Test_strspn_bwd__byte(String src_str, byte find, int bgn, int max, int expd) {
|
||||
Gftest.Eq__int(expd, XophpString.strspn_bwd__byte(Bry_.new_u8(src_str), find, bgn, max));
|
||||
}
|
||||
public void Test_strspn_bwd__space_or_tab(String src_str, int bgn, int max, int expd) {
|
||||
Gftest.Eq__int(expd, XophpString.strspn_bwd__space_or_tab(Bry_.new_u8(src_str), bgn, max));
|
||||
}
|
||||
public void Test_substr(String src_str, int bgn, String expd) {Test_substr(src_str, bgn, String_.Len(src_str), expd);}
|
||||
public void Test_substr(String src_str, int bgn, int len, String expd) {
|
||||
Gftest.Eq__str(expd, XophpString.substr(Bry_.new_u8(src_str), bgn, len));
|
||||
}
|
||||
private Btrie_slim_mgr strtr_trie;
|
||||
public void Init_strtr_by_trie(String... kvs) {
|
||||
if (strtr_trie == null) strtr_trie = Btrie_slim_mgr.cs();
|
||||
int len = kvs.length;
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
strtr_trie.Add_str_str(kvs[i], kvs[i + 1]);
|
||||
}
|
||||
}
|
||||
public void Test_strtr_by_trie(String src, String expd) {
|
||||
Bry_bfr tmp = Bry_bfr_.New();
|
||||
Btrie_rv trv = new Btrie_rv();
|
||||
Gftest.Eq__str(expd, XophpString.strtr(Bry_.new_u8(src), strtr_trie, tmp, trv));
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,6 @@ public class Xop_lxr_ {
|
||||
, Tid_list = 10, Tid_hdr = 11, Tid_hr = 12, Tid_xnde = 13, Tid_lnke_bgn = 14, Tid_lnke_end = 15, Tid_tblw = 16, Tid_pre = 17, Tid_under = 18, Tid_comment = 19
|
||||
, Tid_eq = 20, Tid_curly_bgn = 21, Tid_curly_end = 22, Tid_brack_bgn = 23, Tid_brack_end = 24, Tid_poem = 25
|
||||
, Tid_tvar = 26, Tid_vnt_bgn = 27, Tid_vnt_end = 28, Tid_vnt_eqgt = 29, Tid_vnt_tmpl_bgn = 30, Tid_word = 31, Tid_nl_poem = 32, Tid_cr = 33
|
||||
, Tid_brack_end_lnki = 34, Tid_nl_tab = 35, Tid_escape = 36
|
||||
, Tid_brack_end_lnki = 34, Tid_nl_tab = 35, Tid_escape = 36, Tid_uniq = 37
|
||||
;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.xowa.parsers; import gplx.*; import gplx.xowa.*;
|
||||
import gplx.core.btries.*;
|
||||
import gplx.xowa.langs.*;
|
||||
import gplx.xowa.parsers.apos.*; import gplx.xowa.parsers.amps.*; import gplx.xowa.parsers.lnkes.*; import gplx.xowa.parsers.hdrs.*; import gplx.xowa.parsers.lists.*; import gplx.xowa.parsers.tblws.*; import gplx.xowa.parsers.paras.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.lnkis.*; import gplx.xowa.parsers.tmpls.*; import gplx.xowa.parsers.miscs.*;
|
||||
import gplx.xowa.parsers.apos.*; import gplx.xowa.parsers.amps.*; import gplx.xowa.parsers.lnkes.*; import gplx.xowa.parsers.hdrs.*; import gplx.xowa.parsers.lists.*; import gplx.xowa.parsers.tblws.*; import gplx.xowa.parsers.paras.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.lnkis.*; import gplx.xowa.parsers.tmpls.*; import gplx.xowa.parsers.miscs.*; import gplx.xowa.parsers.uniqs.*;
|
||||
public class Xop_lxr_mgr {
|
||||
private final Xop_lxr[] ary;
|
||||
private final List_adp page_lxr_list = List_adp_.New();
|
||||
@@ -77,6 +77,7 @@ public class Xop_lxr_mgr {
|
||||
, Xop_pre_lxr.Instance, Xop_nl_tab_lxr.Instance
|
||||
, Xop_comm_lxr.Instance
|
||||
, Xop_under_lxr.Instance
|
||||
// , Xop_uniq_lxr.Instance // NOWIKI;DATE:2018-01-16
|
||||
});
|
||||
}
|
||||
public static Xop_lxr_mgr new_anchor_encoder() {
|
||||
|
||||
@@ -15,7 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.parsers; import gplx.*; import gplx.xowa.*;
|
||||
public class Xop_tkn_itm_ {
|
||||
public static final Xop_tkn_itm[] Ary_empty = new Xop_tkn_itm[0];
|
||||
public static final Xop_tkn_itm[] Ary_empty = new Xop_tkn_itm[0];
|
||||
public static final byte
|
||||
Tid_null = 0
|
||||
, Tid_root = 1
|
||||
@@ -69,8 +69,9 @@ public class Xop_tkn_itm_ {
|
||||
, Tid_vnt_eqgt = 49
|
||||
, Tid_cr = 50
|
||||
, Tid_escape = 51
|
||||
, Tid_uniq = 52
|
||||
;
|
||||
public static final String[] Tid__names
|
||||
public static final String[] Tid__names
|
||||
= new String[]
|
||||
{ "null"
|
||||
, "root"
|
||||
@@ -124,5 +125,6 @@ public static final String[] Tid__names
|
||||
, "vnt_eqgt"
|
||||
, "cr"
|
||||
, "escape"
|
||||
, "uniq"
|
||||
};
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ class Xop_section_list implements Xomw_heading_cbk {
|
||||
key = wiki.Parser_mgr().Main().Parse_text_to_html(wiki.Parser_mgr().Ctx(), key);
|
||||
|
||||
// handle math; EX: "== <math>\delta</math> =="
|
||||
key = wiki.Parser_mgr().Uniq_mgr().Convert(key);
|
||||
key = wiki.Parser_mgr().Uniq_mgr().Parse(key);
|
||||
|
||||
// convert key to toc_text to handle (a) XML ("<i>a</i>" -> "a"); (b) dupes ("text" -> "text_2")
|
||||
int num = wkr.Hdr_num();
|
||||
|
||||
@@ -71,7 +71,7 @@ public class Xop_section_mgr implements Gfo_invk {
|
||||
this.fmt__edit_hint.Fmt_(String_.new_u8(wiki.Msg_mgr().Val_by_key_obj("editsectionhint")));
|
||||
}
|
||||
|
||||
section_key = wiki.Parser_mgr().Uniq_mgr().Convert(section_key); // need to swap out uniqs for Math; DATE:2016-12-09
|
||||
section_key = wiki.Parser_mgr().Uniq_mgr().Parse(section_key); // need to swap out uniqs for Math; DATE:2016-12-09
|
||||
byte[] edit_hint = fmt__edit_hint.Bld_many_to_bry(tmp_bfr, section_hint);
|
||||
fmt__section_editable.Bld_many(bfr, page_ttl, section_key, edit_hint, bry__edit_text);
|
||||
}
|
||||
|
||||
@@ -28,22 +28,22 @@ public class Mwh_atr_itm {
|
||||
this.val_bgn = val_bgn; this.val_end = val_end; this.val_bry = val_bry;
|
||||
this.eql_pos = eql_pos; this.qte_tid = qte_tid;
|
||||
}
|
||||
public byte[] Src() {return src;} private final byte[] src;
|
||||
public boolean Valid() {return valid;} private final boolean valid;
|
||||
public boolean Key_exists() {return key_exists;} private final boolean key_exists;
|
||||
public boolean Repeated() {return repeated;} private final boolean repeated;
|
||||
public byte[] Src() {return src;} private final byte[] src;
|
||||
public boolean Valid() {return valid;} private final boolean valid;
|
||||
public boolean Key_exists() {return key_exists;} private final boolean key_exists;
|
||||
public boolean Repeated() {return repeated;} private final boolean repeated;
|
||||
public boolean Invalid() {return repeated || !valid;}
|
||||
public int Atr_bgn() {return atr_bgn;} private int atr_bgn;
|
||||
public int Atr_end() {return atr_end;} private int atr_end;
|
||||
public int Key_bgn() {return key_bgn;} private final int key_bgn;
|
||||
public int Key_end() {return key_end;} private final int key_end;
|
||||
public int Key_bgn() {return key_bgn;} private final int key_bgn;
|
||||
public int Key_end() {return key_end;} private final int key_end;
|
||||
public byte[] Key_bry() {return key_bry;} private byte[] key_bry;
|
||||
public byte Key_tid() {return key_tid;} public Mwh_atr_itm Key_tid_(byte v) {key_tid = v; return this;} private byte key_tid;
|
||||
public int Val_bgn() {return val_bgn;} private final int val_bgn;
|
||||
public int Val_end() {return val_end;} private final int val_end;
|
||||
public int Val_bgn() {return val_bgn;} private final int val_bgn;
|
||||
public int Val_end() {return val_end;} private final int val_end;
|
||||
public byte[] Val_bry() {return val_bry;} private byte[] val_bry;
|
||||
public int Eql_pos() {return eql_pos;} private final int eql_pos;
|
||||
public int Qte_tid() {return qte_tid;} private final int qte_tid;
|
||||
public int Eql_pos() {return eql_pos;} private final int eql_pos;
|
||||
public int Qte_tid() {return qte_tid;} private final int qte_tid;
|
||||
public byte Qte_byte() {
|
||||
switch (qte_tid) {
|
||||
case Mwh_atr_itm_.Qte_tid__none: return Byte_ascii.Null;
|
||||
@@ -53,7 +53,6 @@ public class Mwh_atr_itm {
|
||||
}
|
||||
}
|
||||
public Mwh_atr_itm Atr_rng(int bgn, int end) {this.atr_bgn = bgn; this.atr_end = end; return this;}
|
||||
public void Key_bry_(byte[] v) {this.key_bry = v;}
|
||||
public void Val_bry_(byte[] v) {this.val_bry = v;}
|
||||
public String Val_as_str() {return String_.new_u8(Val_as_bry());}
|
||||
public byte[] Val_as_bry() {if (val_bry == null) val_bry = Bry_.Mid(src, val_bgn, val_end); return val_bry;} // NOTE: val_bry is cached
|
||||
|
||||
@@ -493,6 +493,9 @@ public class Xop_tblw_wkr implements Xop_ctx_wkr {
|
||||
atrs_bgn = Bry_find_.Find_fwd_while(src, atrs_bgn, src.length, Byte_ascii.Dash);
|
||||
prv_tblw.Atrs_rng_set(atrs_bgn, atrs_end);
|
||||
if (ctx.Parse_tid() == Xop_parser_tid_.Tid__wtxt && atrs_bgn != -1) {
|
||||
// NOWIKI;DATE:2018-01-16
|
||||
// byte[] converted = ctx.Wiki().Parser_mgr().Uniq_mgr().Parse(Bool_.N, Bry_.Mid(src, atrs_bgn, atrs_end));
|
||||
// Mwh_atr_itm[] atrs = ctx.App().Parser_mgr().Xnde__parse_atrs(converted, 0, converted.length);
|
||||
Mwh_atr_itm[] atrs = ctx.App().Parser_mgr().Xnde__parse_atrs_for_tblw(src, atrs_bgn, atrs_end);
|
||||
prv_tblw.Atrs_ary_as_tblw_(atrs);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.xowa.parsers.tblws; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
|
||||
import org.junit.*;
|
||||
public class Xop_tblw_wkr__uncommon_tst {
|
||||
@Before public void init() {fxt.Reset(); fxt.Init_para_y_();} private final Xop_fxt fxt = new Xop_fxt();
|
||||
@Before public void init() {fxt.Reset(); fxt.Init_para_y_();} private final Xop_fxt fxt = new Xop_fxt();
|
||||
@After public void term() {fxt.Init_para_n_();}
|
||||
@Test public void Tr_pops_entire_stack() { // PURPOSE: in strange cases, tr will pop entire stack; PAGE:en.w:Turks_in_Denmark; DATE:2014-03-02
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl_skip_last
|
||||
|
||||
@@ -62,8 +62,12 @@ public class Xot_tmpl_wtr {
|
||||
if (xnde.Tag_close_bgn() == Int_.Min_value)
|
||||
rslt_bfr.Add_mid(src, tkn.Src_bgn(), tkn.Src_end()); // write src from bgn/end
|
||||
else { // NOTE: if nowiki then "deactivate" all xndes by swapping out < for < nowiki_xnde_frag; DATE:2013-01-27
|
||||
Bry_bfr tmp_bfr = ctx.Wiki().Utl__bfr_mkr().Get_k004();
|
||||
// NOWIKI;DATE:2018-01-16
|
||||
// byte[] uniq = ctx.Wiki().Parser_mgr().Uniq_mgr().Add(Bool_.N, Bry_.Empty, Bry_.Mid(src, xnde.Tag_open_end(), xnde.Tag_close_bgn()));
|
||||
// rslt_bfr.Add(uniq);
|
||||
|
||||
int nowiki_content_bgn = xnde.Tag_open_end(), nowiki_content_end = xnde.Tag_close_bgn();
|
||||
Bry_bfr tmp_bfr = ctx.Wiki().Utl__bfr_mkr().Get_k004();
|
||||
boolean escaped = gplx.xowa.parsers.tmpls.Nowiki_escape_itm.Escape(tmp_bfr, src, nowiki_content_bgn, nowiki_content_end);
|
||||
rslt_bfr.Add_bfr_or_mid(escaped, tmp_bfr, src, nowiki_content_bgn, nowiki_content_end);
|
||||
tmp_bfr.Mkr_rls();
|
||||
|
||||
30
400_xowa/src/gplx/xowa/parsers/uniqs/Xop_uniq_itm.java
Normal file
30
400_xowa/src/gplx/xowa/parsers/uniqs/Xop_uniq_itm.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.parsers.uniqs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
|
||||
class Xop_uniq_itm {
|
||||
public Xop_uniq_itm(boolean expand_after_template_parsing, byte[] type, int idx, byte[] key, byte[] val) {
|
||||
this.expand_after_template_parsing = expand_after_template_parsing;
|
||||
this.type = type;
|
||||
this.idx = idx;
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
public boolean Expand_after_template_parsing() {return expand_after_template_parsing;} private final boolean expand_after_template_parsing;
|
||||
public byte[] Type() {return type;} private final byte[] type;
|
||||
public int Idx() {return idx;} private final int idx;
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public byte[] Val() {return val;} private final byte[] val;
|
||||
}
|
||||
41
400_xowa/src/gplx/xowa/parsers/uniqs/Xop_uniq_lxr.java
Normal file
41
400_xowa/src/gplx/xowa/parsers/uniqs/Xop_uniq_lxr.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.parsers.uniqs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
|
||||
import gplx.core.btries.*; import gplx.xowa.langs.*;
|
||||
// EX: "\u007fUNIQ-item-1-QINU\u007f"
|
||||
public class Xop_uniq_lxr implements Xop_lxr {
|
||||
public int Lxr_tid() {return Xop_lxr_.Tid_uniq;}
|
||||
public void Init_by_wiki(Xowe_wiki wiki, Btrie_fast_mgr core_trie) {
|
||||
core_trie.Add(Xop_uniq_mgr.Bry__uniq__bgn_w_dash, this);
|
||||
}
|
||||
public void Init_by_lang(Xol_lang_itm lang, Btrie_fast_mgr core_trie) {}
|
||||
public void Term(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) {
|
||||
// find end
|
||||
int rhs_bgn = Bry_find_.Find_fwd(src, Xop_uniq_mgr.Bry__uniq__add__end, cur_pos);
|
||||
if (rhs_bgn == Bry_find_.Not_found) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "uniq_mgr:unable to find uniq; src=~{0}", src);
|
||||
return ctx.Lxr_make_txt_(cur_pos);
|
||||
}
|
||||
int rhs_end = rhs_bgn + Xop_uniq_mgr.Bry__uniq__add__end.length;
|
||||
|
||||
byte[] key = Bry_.Mid(src, bgn_pos, rhs_end);
|
||||
Xop_uniq_tkn uniq_tkn = new Xop_uniq_tkn(bgn_pos, rhs_end, key);
|
||||
ctx.Subs_add(root, uniq_tkn);
|
||||
return rhs_end;
|
||||
}
|
||||
public static final Xop_uniq_lxr Instance = new Xop_uniq_lxr(); Xop_uniq_lxr() {}
|
||||
}
|
||||
@@ -17,88 +17,73 @@ package gplx.xowa.parsers.uniqs; import gplx.*; import gplx.xowa.*; import gplx.
|
||||
import gplx.core.btries.*;
|
||||
public class Xop_uniq_mgr { // REF.MW:/parser/StripState.php
|
||||
private final Btrie_slim_mgr general_trie = Btrie_slim_mgr.cs(); private final Btrie_rv trv = new Btrie_rv();
|
||||
private final Bry_bfr key_bfr = Bry_bfr_.New_w_size(32);
|
||||
private int idx = -1;
|
||||
public void Clear() {idx = -1; general_trie.Clear();}
|
||||
public byte[] Get(byte[] key) {return (byte[])general_trie.Match_exact(key, 0, key.length);}
|
||||
public byte[] Add(byte[] type, byte[] val) {// "<b>" -> "\u007fUNIQ-item-1--QINU\u007f"
|
||||
byte[] key = key_bfr
|
||||
.Add(Bry__uniq__bgn_w_dash)
|
||||
.Add(type).Add_byte(Byte_ascii.Dash) // EX: "ref-"
|
||||
.Add_int_variable(++idx)
|
||||
.Add(Bry__uniq__add__end).To_bry_and_clear();
|
||||
general_trie.Add_bry_bry(key, val);
|
||||
return key;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New_w_size(32);
|
||||
private int nxt_idx = -1;
|
||||
public void Clear() {
|
||||
nxt_idx = -1;
|
||||
general_trie.Clear();
|
||||
}
|
||||
public byte[] Convert(byte[] src) {
|
||||
if (general_trie.Count() == 0) return src;
|
||||
|
||||
Bry_bfr dirty_bfr = null;
|
||||
int cur = 0;
|
||||
int len = src.length;
|
||||
while (cur < len) {
|
||||
// find "\u007fUNIQ-"
|
||||
int uniq_bgn = Bry_find_.Find_fwd(src, Bry__uniq__bgn_w_dash, cur);
|
||||
if (uniq_bgn == Bry_find_.Not_found) break;
|
||||
|
||||
// find "-"; EX: ref-
|
||||
int tmp_pos = uniq_bgn;
|
||||
tmp_pos = Bry_find_.Find_fwd(src, Byte_ascii.Dash, tmp_pos, len);
|
||||
if (tmp_pos == Bry_find_.Not_found) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "uniq_mgr:unable to find 2nd dash; src=~{0}", src);
|
||||
return src;
|
||||
}
|
||||
|
||||
// find end
|
||||
int uniq_end = Bry_find_.Find_fwd(src, Bry__uniq__add__end, tmp_pos);
|
||||
if (uniq_end == Bry_find_.Not_found) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "uniq_mgr:unable to convert uniq; src=~{0}", src);
|
||||
return src;
|
||||
}
|
||||
uniq_end += Bry__uniq__add__end.length;
|
||||
|
||||
// add to bfr
|
||||
if (dirty_bfr == null) dirty_bfr = key_bfr;
|
||||
dirty_bfr.Add_mid(src, cur, uniq_bgn);
|
||||
dirty_bfr.Add((byte[])general_trie.Match_exact(src, uniq_bgn, uniq_end));
|
||||
cur = uniq_end;
|
||||
}
|
||||
|
||||
if (dirty_bfr != null) {
|
||||
dirty_bfr.Add_mid(src, cur, len);
|
||||
}
|
||||
return dirty_bfr == null ? src : dirty_bfr.To_bry_and_clear();
|
||||
public byte[] Get(byte[] key) {
|
||||
Xop_uniq_itm itm = (Xop_uniq_itm)general_trie.Match_exact(key, 0, key.length);
|
||||
return itm.Val();
|
||||
}
|
||||
public byte[] Add(boolean expand_after_template_parsing, byte[] type, byte[] val) {// "<b>" -> "\u007fUNIQ-item-1-QINU\u007f"
|
||||
int idx = ++nxt_idx;
|
||||
byte[] key = tmp_bfr
|
||||
.Add(Bry__uniq__bgn_w_dash) // "\u007f'\"`UNIQ-"
|
||||
.Add(type).Add_byte(Byte_ascii.Dash) // "ref-"
|
||||
.Add_int_variable(idx) // "1"
|
||||
.Add(Bry__uniq__add__end) // "-QINU`\"'\u007f"
|
||||
.To_bry_and_clear();
|
||||
Xop_uniq_itm itm = new Xop_uniq_itm(expand_after_template_parsing, type, idx, key, val);
|
||||
general_trie.Add_obj(key, itm);
|
||||
return key;
|
||||
}
|
||||
public void Parse(Bry_bfr bfr) {
|
||||
if (general_trie.Count() == 0) return;
|
||||
byte[] rv = Parse(key_bfr, general_trie, bfr.To_bry_and_clear());
|
||||
byte[] rv = Parse_recurse(Bool_.Y, tmp_bfr, bfr.To_bry_and_clear());
|
||||
bfr.Add(rv);
|
||||
}
|
||||
public byte[] Parse(byte[] src) {return Parse(key_bfr, general_trie, src);}
|
||||
private byte[] Parse(Bry_bfr bfr, Btrie_slim_mgr trie, byte[] src) {
|
||||
public byte[] Parse(boolean template_parsing, byte[] src) {return Parse_recurse(template_parsing, tmp_bfr, src);}
|
||||
public byte[] Parse(byte[] src) {return Parse_recurse(Bool_.Y, tmp_bfr, src);}
|
||||
private byte[] Parse_recurse(boolean template_parsing, Bry_bfr bfr, byte[] src) {
|
||||
int src_len = src.length;
|
||||
int pos = 0;
|
||||
int mark_bgn = 0;
|
||||
int prv_bgn = 0;
|
||||
boolean dirty = false;
|
||||
|
||||
while (true) {
|
||||
boolean is_last = pos == src_len;
|
||||
byte b = is_last ? Byte_ascii.Null : src[pos];
|
||||
Object o = trie.Match_at_w_b0(trv, b, src, pos, src_len);
|
||||
Object o = general_trie.Match_at_w_b0(trv, b, src, pos, src_len);
|
||||
|
||||
// match not found for "\x7fUNIQ"; move on to next
|
||||
if (o == null)
|
||||
++pos;
|
||||
pos++;
|
||||
// match found
|
||||
else {
|
||||
byte[] val = (byte[])o;
|
||||
int new_pos = trv.Pos(); // NOTE: since trie is reused, must capture pos here
|
||||
val = Parse(Bry_bfr_.New(), trie, val);
|
||||
Xop_uniq_itm itm = (Xop_uniq_itm)o;
|
||||
int itm_end = trv.Pos(); // NOTE: must capture pos since trv is reused in the recursive call below
|
||||
// skip if template_parsing
|
||||
if (template_parsing
|
||||
&& !itm.Expand_after_template_parsing()) {
|
||||
pos = itm_end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// add everything from prv_bgn up to UNIQ
|
||||
bfr.Add_mid(src, prv_bgn, pos);
|
||||
|
||||
// expand UNIQ (can be recursive)
|
||||
byte[] val = Parse_recurse(template_parsing, Bry_bfr_.New(), itm.Val());
|
||||
// val = gplx.xowa.parsers.xndes.Xop_xnde_tkn.Hack_ctx.Wiki().Parser_mgr().Main().Parse_text_to_html(gplx.xowa.parsers.xndes.Xop_xnde_tkn.Hack_ctx, val); // CHART
|
||||
bfr.Add_mid(src, mark_bgn, pos);
|
||||
bfr.Add(val);
|
||||
dirty = true;
|
||||
pos = mark_bgn = new_pos;
|
||||
pos = prv_bgn = itm_end;
|
||||
}
|
||||
if (is_last) {
|
||||
if (dirty)
|
||||
bfr.Add_mid(src, mark_bgn, src_len);
|
||||
bfr.Add_mid(src, prv_bgn, src_len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -111,19 +96,19 @@ public class Xop_uniq_mgr { // REF.MW:/parser/StripState.php
|
||||
}
|
||||
public void Random_int_ary_(int... v) {random_int_ary = v;} private int[] random_int_ary; // TEST:
|
||||
public byte[] Random_bry_new(int len) {
|
||||
Bry_bfr key_bfr = Bry_bfr_.New();
|
||||
Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
RandomAdp random_gen = RandomAdp_.new_();
|
||||
for (int i = 0; i < len; i += 7) {
|
||||
int rand = random_int_ary == null ? random_gen.Next(Int_.Max_value) : random_int_ary[i / 7];
|
||||
String rand_str = Int_.To_str_hex(Bool_.N, Bool_.Y, rand & 0xfffffff); // limits value to 268435455
|
||||
key_bfr.Add_str_a7(rand_str);
|
||||
tmp_bfr.Add_str_a7(rand_str);
|
||||
}
|
||||
byte[] rv = key_bfr.To_bry(0, len);
|
||||
key_bfr.Clear();
|
||||
byte[] rv = tmp_bfr.To_bry(0, len);
|
||||
tmp_bfr.Clear();
|
||||
return rv;
|
||||
}
|
||||
|
||||
private static final byte[]
|
||||
public static final byte[]
|
||||
Bry__uniq__bgn = Bry_.new_a7("\u007f'\"`UNIQ-")
|
||||
, Bry__uniq__bgn_w_dash = Bry_.Add(Bry__uniq__bgn, Byte_ascii.Dash_bry)
|
||||
, Bry__uniq__add__end = Bry_.new_a7("-QINU`\"'\u007f")
|
||||
|
||||
@@ -65,7 +65,7 @@ class Xop_uniq_mgr__fxt {
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Uniq_bry_new()), "unique_bry");
|
||||
}
|
||||
public void Test__add(String raw, String expd) {
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Add(Bry_.new_a7("item"), Bry_.new_a7(raw))), "add");
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Add(Bool_.Y, Bry_.new_a7("item"), Bry_.new_a7(raw))), "add");
|
||||
}
|
||||
public void Test__get(String key, String expd) {
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Get(Bry_.new_a7(key))), "get");
|
||||
@@ -74,6 +74,6 @@ class Xop_uniq_mgr__fxt {
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Parse(Bry_.new_a7(raw))), "parse");
|
||||
}
|
||||
public void Test__convert(String raw, String expd) {
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Convert(Bry_.new_a7(raw))), "convert");
|
||||
Gftest.Eq__str(expd, String_.new_a7(mgr.Parse(Bry_.new_a7(raw))), "convert");
|
||||
}
|
||||
}
|
||||
|
||||
26
400_xowa/src/gplx/xowa/parsers/uniqs/Xop_uniq_tkn.java
Normal file
26
400_xowa/src/gplx/xowa/parsers/uniqs/Xop_uniq_tkn.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.parsers.uniqs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
|
||||
import gplx.langs.htmls.entitys.*;
|
||||
// EX: "\u007fUNIQ-item-1--QINU\u007f"
|
||||
public class Xop_uniq_tkn extends Xop_tkn_itm_base {
|
||||
public Xop_uniq_tkn(int bgn, int end, byte[] key) {
|
||||
this.Tkn_ini_pos(false, bgn, end);
|
||||
this.key = key;
|
||||
}
|
||||
@Override public byte Tkn_tid() {return Xop_tkn_itm_.Tid_uniq;}
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
}
|
||||
@@ -18,7 +18,8 @@ import gplx.core.primitives.*; import gplx.core.btries.*; import gplx.xowa.parse
|
||||
public class Xop_xatr_whitelist_mgr {
|
||||
private final Hash_adp_bry grp_hash = Hash_adp_bry.cs();
|
||||
private final Btrie_rv trv = new Btrie_rv();
|
||||
public boolean Chk(int tag_id, byte[] src, Mwh_atr_itm xatr) {
|
||||
public boolean Chk(int tag_id, Mwh_atr_itm xatr) {
|
||||
byte[] src = xatr.Src();
|
||||
byte[] key_bry = xatr.Key_bry();
|
||||
byte[] chk_bry; int chk_bgn, chk_end;
|
||||
if (key_bry == null) {
|
||||
|
||||
@@ -16,7 +16,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.xowa.parsers.xndes; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
|
||||
import org.junit.*; import gplx.xowa.parsers.htmls.*;
|
||||
public class Xop_xatr_whitelist_mgr_tst {
|
||||
private final Xop_xatr_whitelist_fxt fxt = new Xop_xatr_whitelist_fxt();
|
||||
private final Xop_xatr_whitelist_fxt fxt = new Xop_xatr_whitelist_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Basic() {
|
||||
fxt.Whitelist(Xop_xnde_tag_.Tid__div , "style" , true);
|
||||
@@ -45,29 +45,29 @@ public class Xop_xatr_whitelist_mgr_tst {
|
||||
}
|
||||
class Xop_xatr_whitelist_fxt {
|
||||
private Xop_xatr_whitelist_mgr whitelist_mgr;
|
||||
private Mwh_atr_itm atr_itm = new Mwh_atr_itm(null, false, false, false, -1, -1, -1, -1, null, -1, -1, null, -1, 0);
|
||||
public void Clear() {
|
||||
if (whitelist_mgr == null) whitelist_mgr = new Xop_xatr_whitelist_mgr().Ini();
|
||||
}
|
||||
public void Whitelist(int tag_id, String key_str, boolean expd) {
|
||||
byte[] key_bry = Bry_.new_a7(key_str);
|
||||
// atr_itm.Key_rng_(0, key_bry.length);
|
||||
atr_itm.Key_bry_(key_bry);
|
||||
Tfds.Eq(expd, whitelist_mgr.Chk(tag_id, key_bry, atr_itm), key_str);
|
||||
Mwh_atr_itm atr_itm = New_atr_itm(key_str, null);
|
||||
Tfds.Eq(expd, whitelist_mgr.Chk(tag_id, atr_itm), key_str);
|
||||
}
|
||||
public void Whitelist(int tag_id, String key_str, String val_str, boolean expd) {
|
||||
byte[] key_bry = Bry_.new_a7(key_str);
|
||||
// atr_itm.Key_rng_(0, key_bry.length);
|
||||
atr_itm.Key_bry_(key_bry);
|
||||
atr_itm.Val_bry_(Bry_.new_a7(val_str));
|
||||
Tfds.Eq(expd, whitelist_mgr.Chk(tag_id, key_bry, atr_itm), key_str);
|
||||
Mwh_atr_itm atr_itm = New_atr_itm(key_str, val_str);
|
||||
Tfds.Eq(expd, whitelist_mgr.Chk(tag_id, atr_itm), key_str);
|
||||
}
|
||||
public void Scrub_style_pass(String style_val_str) {Scrub_style(style_val_str, style_val_str);}
|
||||
public void Scrub_style_fail(String val_str) {Scrub_style(val_str, "");}
|
||||
public void Scrub_style(String val_str, String expd) {
|
||||
byte[] val_bry = Bry_.new_a7(val_str);
|
||||
atr_itm.Val_bry_(val_bry);
|
||||
Mwh_atr_itm atr_itm = New_atr_itm(null, val_str);
|
||||
whitelist_mgr.Scrub_style(atr_itm, val_bry);
|
||||
Tfds.Eq(expd, String_.new_a7(atr_itm.Val_bry()));
|
||||
}
|
||||
private static Mwh_atr_itm New_atr_itm(String key_str, String val_str) {
|
||||
byte[] key_bry = key_str == null ? null : Bry_.new_u8(key_str);
|
||||
byte[] val_bry = val_str == null ? null : Bry_.new_u8(val_str);
|
||||
Mwh_atr_itm rv = new Mwh_atr_itm(key_bry, false, false, false, -1, -1, -1, -1, key_bry, -1, -1, val_bry, -1, 0);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class Xop_xnde_tkn extends Xop_tkn_itm_base implements Xop_tblw_tkn {
|
||||
// UNIQ; DATE:2017-03-31
|
||||
if (is_tmpl_mode) {
|
||||
byte[] val = cur_bfr.To_bry_and_clear();
|
||||
byte[] key = ctx.Wiki().Parser_mgr().Uniq_mgr().Add(tag.Name_bry(), val);
|
||||
byte[] key = ctx.Wiki().Parser_mgr().Uniq_mgr().Add(Bool_.Y, tag.Name_bry(), val);
|
||||
bfr.Add(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +302,13 @@ public class Xop_xnde_wkr implements Xop_ctx_wkr {
|
||||
}
|
||||
Mwh_atr_itm[] atrs = null;
|
||||
if (ctx.Parse_tid() == Xop_parser_tid_.Tid__wtxt) {
|
||||
// NOWIKI;DATE:2018-01-16
|
||||
// if (atrs_bgn < atrs_end) {
|
||||
// byte[] converted = ctx.Wiki().Parser_mgr().Uniq_mgr().Parse(Bool_.N, Bry_.Mid(src, atrs_bgn, atrs_end));
|
||||
// atrs = ctx.App().Parser_mgr().Xnde__parse_atrs(converted, 0, converted.length);
|
||||
// }
|
||||
// else
|
||||
// atrs = ctx.App().Parser_mgr().Xnde__parse_atrs(src, atrs_bgn, atrs_end);
|
||||
atrs = ctx.App().Parser_mgr().Xnde__parse_atrs(src, atrs_bgn, atrs_end);
|
||||
}
|
||||
if (( ( tag.Xtn()
|
||||
|
||||
@@ -16,7 +16,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.xowa.parsers.xndes; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
|
||||
import org.junit.*;
|
||||
public class Xop_xnde_wkr__nowiki_tst {
|
||||
private final Xop_fxt fxt = new Xop_fxt();
|
||||
private final Xop_fxt fxt = new Xop_fxt();
|
||||
@After public void term() {fxt.Init_para_n_();}
|
||||
@Test public void Basic() {
|
||||
fxt.Test_parse_page_wiki_str
|
||||
|
||||
@@ -175,7 +175,7 @@ public class Gallery_mgr_wtr {
|
||||
int len = xatr_list.Count();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Mwh_atr_itm xatr = (Mwh_atr_itm)xatr_list.Get_at(i);
|
||||
if (!whitelist_mgr.Chk(Xop_xnde_tag_.Tid__ul, src, xatr)) continue;
|
||||
if (!whitelist_mgr.Chk(Xop_xnde_tag_.Tid__ul, xatr)) continue;
|
||||
byte[] key = xatr.Key_bry();
|
||||
byte[] val = xatr.Val_as_bry();
|
||||
Gfh_wtr.Write_atr_bry(bfr, key, val);
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.jsonConfigs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.jsonConfigs.*;
|
||||
import gplx.xowa.langs.*;
|
||||
class Jscfg_localizer {
|
||||
public Keyval[] Localize(Xol_lang_itm lang, byte[] page, Keyval[] root) {
|
||||
if (lang == null) return root; // if no lang, return original
|
||||
|
||||
int len = root.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Keyval nde = root[i];
|
||||
String nde_key = nde.Key();
|
||||
if (String_.Eq(nde_key, Id__root__license)) {
|
||||
}
|
||||
else if (String_.Eq(nde_key, Id__root__description)) {
|
||||
root[i] = pickLocalizedString(lang, page, nde);
|
||||
}
|
||||
else if (String_.Eq(nde_key, Id__root__schema)) {
|
||||
nde = Localize_schema(lang, page, nde);
|
||||
}
|
||||
else if (String_.Eq(nde_key, Id__root__data)) {
|
||||
nde = Localize_data(lang, page, nde);
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
private Keyval Localize_schema(Xol_lang_itm lang, byte[] page, Keyval schema) {
|
||||
Keyval[] schemas = Cast_to_kvary_or_null(page, schema) ; if (schemas == null) return schema;
|
||||
Keyval[] fields = Cast_to_kvary_or_null(page, schemas[0]); if (fields == null) return schema;
|
||||
for (Keyval field : fields) {
|
||||
Keyval[] atrs = (Keyval[])field.Val();
|
||||
int atrs_len = atrs.length;
|
||||
for (int i = 0; i < atrs_len; i++) {
|
||||
Keyval atr = atrs[i];
|
||||
if (String_.Eq(atr.Key(), Id__fld__title)) {
|
||||
atrs[i] = pickLocalizedString(lang, page, atr);
|
||||
}
|
||||
}
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
private Keyval Localize_data(Xol_lang_itm lang, byte[] page, Keyval data) {
|
||||
Keyval[] rows = Cast_to_kvary_or_null(page, data); if (rows == null) return data;
|
||||
for (Keyval row : rows) {
|
||||
Object[] vals = (Object[])row.Val();
|
||||
int len = vals.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Object val = vals[i];
|
||||
if (Type_.Eq_by_obj(val, Keyval[].class)) {
|
||||
Keyval val_as_kv = pickLocalizedString(lang, Int_.To_str(i), (Keyval[])val);
|
||||
vals[i] = val_as_kv.Val();
|
||||
}
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
private static Keyval pickLocalizedString(Xol_lang_itm lang, byte[] page, Keyval kv) {
|
||||
Keyval[] kvs = Cast_to_kvary_or_null(page, kv.Key(), kv.Val());
|
||||
Keyval rv = pickLocalizedString(lang, kv.Key(), kvs);
|
||||
return rv == null ? kv : rv;
|
||||
}
|
||||
public static Keyval pickLocalizedString(Xol_lang_itm lang, String key, Keyval[] ary) {
|
||||
// local vars for conditional logic
|
||||
Object val_lang = null, val_en = null, val_1st = null;
|
||||
Object[] val_fallbacks = null;
|
||||
|
||||
// local vars for lang
|
||||
String langCode = lang.Key_str();
|
||||
Ordered_hash fallback_hash = lang.Fallback_hash();
|
||||
|
||||
// loop ary to populate local vars
|
||||
for (Keyval itm : ary) {
|
||||
String itm_key = itm.Key();
|
||||
Object itm_val = itm.Val();
|
||||
if (val_1st == null) {
|
||||
val_1st = itm_val;
|
||||
}
|
||||
if (String_.Eq(itm_key, langCode)) {
|
||||
val_lang = itm_val;
|
||||
}
|
||||
else if (fallback_hash.Has(itm_key)) {
|
||||
if (val_fallbacks == null) {
|
||||
val_fallbacks = new Object[fallback_hash.Len()];
|
||||
}
|
||||
int idx = fallback_hash.Idx_of(itm_key);
|
||||
val_fallbacks[idx] = itm_val;
|
||||
}
|
||||
else if (String_.Eq(itm_key, "en")) {
|
||||
val_en = itm_val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (val_lang != null) {
|
||||
return Keyval_.new_(key, val_lang);
|
||||
}
|
||||
|
||||
if (val_fallbacks != null) {
|
||||
for (Object v : val_fallbacks) {
|
||||
if (v != null)
|
||||
return Keyval_.new_(key, v);
|
||||
}
|
||||
}
|
||||
|
||||
// If fallbacks fail, check if english is defined
|
||||
if (val_en != null) {
|
||||
return Keyval_.new_(key, val_en);
|
||||
}
|
||||
|
||||
// We have a custom default, return that
|
||||
// if (defaultValue != null) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// Return first available value, or an empty String
|
||||
// There might be a better way to get the first value from an Object
|
||||
Object val = val_1st == null ? "" : val_1st;
|
||||
return Keyval_.new_(key, val);
|
||||
}
|
||||
private static Keyval[] Cast_to_kvary_or_null(byte[] page, Keyval kv) {return Cast_to_kvary_or_null(page, kv.Key(), kv.Val());}
|
||||
private static Keyval[] Cast_to_kvary_or_null(byte[] page, String key, Object val) {
|
||||
if (Type_.Eq_by_obj(val, Keyval[].class)) {
|
||||
return (Keyval[])val;
|
||||
}
|
||||
else {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "could not cast to kvary; page=~{0} key=~{1}", key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private static final String
|
||||
Id__root__schema = "schema"
|
||||
, Id__root__data = "data"
|
||||
, Id__root__description = "description"
|
||||
, Id__root__license = "license"
|
||||
, Id__fld__title = "title"
|
||||
;
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*; impor
|
||||
import gplx.xowa.wikis.domains.*;
|
||||
public class Jscfg_scrib_lib implements Scrib_lib {
|
||||
private final Scrib_lib_text__json_util json_util = new Scrib_lib_text__json_util();
|
||||
private final Jscfg_localizer localizer = new Jscfg_localizer();
|
||||
private Scrib_core core;
|
||||
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
|
||||
public Scrib_lib Init() {procs.Init_by_lib(this, Proc_names); return this;}
|
||||
@@ -57,6 +58,8 @@ public class Jscfg_scrib_lib implements Scrib_lib {
|
||||
throw Err_.new_wo_type("bad argument #1 to 'get' (not a valid title) " + String_.new_u8(ttl_bry));
|
||||
}
|
||||
|
||||
return Scrib_lib_text.JsonDecodeStatic(args, rslt, core, json_util, page, Scrib_lib_text__json_util.Opt__force_assoc, Scrib_lib_text__json_util.Flag__none);
|
||||
Keyval[] rv = Scrib_lib_text.JsonDecodeStatic(args, core, json_util, page, Scrib_lib_text__json_util.Opt__force_assoc, Scrib_lib_text__json_util.Flag__none);
|
||||
rv = localizer.Localize(core.Wiki().Lang(), page, rv);
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,18 +14,14 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.jsonConfigs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.jsonConfigs.*;
|
||||
import org.junit.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*;
|
||||
import gplx.xowa.langs.*;
|
||||
import gplx.langs.jsons.*;
|
||||
public class Jscfg_scrib_lib_tst {
|
||||
@Before public void init() {
|
||||
fxt.Clear_for_lib();
|
||||
lib = new Jscfg_scrib_lib();
|
||||
lib.Init();
|
||||
lib.Core_(fxt.Core());
|
||||
} private Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); private Jscfg_scrib_lib lib;
|
||||
private final Jscfg_scrib_lib_fxt fxt = new Jscfg_scrib_lib_fxt();
|
||||
@Test public void Get() {
|
||||
Xowe_wiki commons_wiki = fxt.Parser_fxt().Wiki().Appe().Wiki_mgr().Get_by_or_make(gplx.xowa.wikis.domains.Xow_domain_itm_.Bry__commons).Init_assert();
|
||||
fxt.Parser_fxt().Init_page_create(commons_wiki, "Data:Test.tab", gplx.langs.jsons.Json_doc.Make_str_by_apos
|
||||
fxt.Init__page("Data:Test.tab", Json_doc.Make_str_by_apos
|
||||
( "{"
|
||||
, " 'data':"
|
||||
, " ["
|
||||
@@ -41,7 +37,7 @@ public class Jscfg_scrib_lib_tst {
|
||||
, " ]"
|
||||
, "}"
|
||||
));
|
||||
fxt.Test_scrib_proc_str_ary(lib, Jscfg_scrib_lib.Invk_get, Keyval_.Ary(Keyval_.int_(1, "Test.tab")), String_.Concat_lines_nl_skip_last
|
||||
fxt.Test__get("Test.tab", String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
, " data="
|
||||
, " 1="
|
||||
@@ -52,4 +48,126 @@ public class Jscfg_scrib_lib_tst {
|
||||
, " 2=Data:Q2"
|
||||
));
|
||||
}
|
||||
@Test public void Get_localize() {
|
||||
fxt.Init__page("Data:Test_localize.tab", Json_doc.Make_str_by_apos
|
||||
( "{"
|
||||
, " 'license': 'CC0-1.0',"
|
||||
, " 'description': {"
|
||||
, " 'de': 'Objekttabelle',"
|
||||
, " 'en': 'Object table'"
|
||||
, " },"
|
||||
, " 'sources': 'Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]',"
|
||||
, " 'schema': {"
|
||||
, " 'fields': ["
|
||||
, " {"
|
||||
, " 'name': 'wikidataID',"
|
||||
, " 'type': 'String',"
|
||||
, " 'title': {"
|
||||
, " 'de': 'Wikidata-Item',"
|
||||
, " 'en': 'Wikidata item'"
|
||||
, " }"
|
||||
, " },"
|
||||
, " {"
|
||||
, " 'name': 'wikidataLabel',"
|
||||
, " 'type': 'localized',"
|
||||
, " 'title': {"
|
||||
, " 'de': 'Wikidata-Label',"
|
||||
, " 'en': 'Wikidata label'"
|
||||
, " }"
|
||||
, " }"
|
||||
, " ]"
|
||||
, " },"
|
||||
, " 'data': ["
|
||||
, " ["
|
||||
, " 'Q183',"
|
||||
, " {"
|
||||
, " 'de': 'Deutschland',"
|
||||
, " 'en': 'Germany'"
|
||||
, " }"
|
||||
, " ],"
|
||||
, " ["
|
||||
, " 'Q61912',"
|
||||
, " {"
|
||||
, " 'de': 'Wertheim',"
|
||||
, " 'en': 'Wertheim am Main'"
|
||||
, " }"
|
||||
, " ]"
|
||||
, " ]"
|
||||
, "}"
|
||||
));
|
||||
fxt.Test__get( "Test_localize.tab", String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
, " license=CC0-1.0"
|
||||
, " description=Object table"
|
||||
, " sources=Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]"
|
||||
, " schema="
|
||||
, " fields="
|
||||
, " 1="
|
||||
, " name=wikidataID"
|
||||
, " type=String"
|
||||
, " title=Wikidata item"
|
||||
, " 2="
|
||||
, " name=wikidataLabel"
|
||||
, " type=localized"
|
||||
, " title=Wikidata label"
|
||||
, " data="
|
||||
, " 1="
|
||||
, " 1=Q183"
|
||||
, " 2=Germany"
|
||||
, " 2="
|
||||
, " 1=Q61912"
|
||||
, " 2=Wertheim am Main"
|
||||
));
|
||||
}
|
||||
@Test public void pickLocalizedString() {
|
||||
Xol_lang_itm lang = fxt.Init__lang("zh-cn", "zh1,zh0");
|
||||
|
||||
// match key
|
||||
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "zh-cn"), "zh-cn");
|
||||
|
||||
// match fallback; note that zh1 is higher in fallback list, but lower in kvs
|
||||
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("zh0", "zh1"), "zh1");
|
||||
|
||||
// match en if no key or fallbacks
|
||||
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "en"), "en");
|
||||
|
||||
// pick 1st if no match
|
||||
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "de"), "fr");
|
||||
}
|
||||
}
|
||||
class Jscfg_scrib_lib_fxt {
|
||||
private final Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt();
|
||||
private final Jscfg_scrib_lib lib;
|
||||
private final Xowe_wiki commons_wiki;
|
||||
public Jscfg_scrib_lib_fxt() {
|
||||
fxt.Clear_for_lib();
|
||||
lib = new Jscfg_scrib_lib();
|
||||
lib.Init();
|
||||
lib.Core_(fxt.Core());
|
||||
this.commons_wiki = fxt.Parser_fxt().Wiki().Appe().Wiki_mgr().Get_by_or_make(gplx.xowa.wikis.domains.Xow_domain_itm_.Bry__commons).Init_assert();
|
||||
}
|
||||
public void Init__page(String page, String text) {
|
||||
fxt.Parser_fxt().Init_page_create(commons_wiki, page, text);
|
||||
}
|
||||
public Xol_lang_itm Init__lang(String key, String fallbacks) {
|
||||
Xol_lang_itm lang = new Xol_lang_itm(fxt.Core().App().Lang_mgr(), Bry_.new_u8(key));
|
||||
lang.Fallback_bry_(Bry_.new_a7(fallbacks));
|
||||
return lang;
|
||||
}
|
||||
public Keyval[] Init__picklocalizedStringKvs(String... vals) {
|
||||
int len = vals.length;
|
||||
Keyval[] rv = new Keyval[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
String val = vals[i];
|
||||
rv[i] = Keyval_.new_(val, val);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Test__get(String page, String expd) {
|
||||
fxt.Test_scrib_proc_str_ary(lib, Jscfg_scrib_lib.Invk_get, Keyval_.Ary(Keyval_.int_(1, page)), expd);
|
||||
}
|
||||
public void Test__pickLocalizedString(Xol_lang_itm lang, Keyval[] kv_ary, String expd) {
|
||||
Keyval actl_kv = Jscfg_localizer.pickLocalizedString(lang, "key", kv_ary);
|
||||
Gftest.Eq__obj_or_null(expd, actl_kv.Val());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,23 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.jsonConfigs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.jsonConfigs.*;
|
||||
import gplx.xowa.xtns.scribunto.*;
|
||||
import gplx.xowa.mediawiki.*;
|
||||
public class Jscfg_xtn_mgr extends Xox_mgr_base {
|
||||
@Override public byte[] Xtn_key() {return XTN_KEY;} public static final byte[] XTN_KEY = Bry_.new_a7("JsonConfig");
|
||||
@Override public void Xtn_init_by_wiki(Xowe_wiki wiki) {
|
||||
Scrib_xtn_mgr scrib_xtn = (Scrib_xtn_mgr)wiki.Xtn_mgr().Get_or_fail(Scrib_xtn_mgr.XTN_KEY);
|
||||
scrib_xtn.Lib_mgr().Add(new Jscfg_scrib_lib());
|
||||
}
|
||||
/*
|
||||
@Override public void Xtn_ctor_by_app(Xoae_app app) {
|
||||
Init_xtn();
|
||||
}
|
||||
public void Init_xtn() {
|
||||
JCSingleton singleton = new JCSingleton();
|
||||
singleton.ConfigModels().Add(JCTabularContent.Model_id, JCTabularContent.Model_id);
|
||||
XophpEnv.Instance.Singletons().Add(JCSingleton.Singleton_Id, singleton);
|
||||
XophpEnv.Instance.ClassBldrs().Add(JCTabularContent.Model_id, new JCTabularContentFactory());
|
||||
}
|
||||
*/
|
||||
@Override public Xox_mgr Xtn_clone_new() {return new Jscfg_xtn_mgr();}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class Xomath_html_wtr {
|
||||
}
|
||||
|
||||
// write html: <span>math_expr</math>
|
||||
byte[] unique_bry = wiki.Parser_mgr().Uniq_mgr().Add(Bry__math, math_bry);
|
||||
byte[] unique_bry = wiki.Parser_mgr().Uniq_mgr().Add(Bool_.Y, Bry__math, math_bry);
|
||||
Bry_fmt fmt = is_latex ? fmt__latex : fmt__mathjax;
|
||||
fmt.Bld_many(tmp_bfr, uid, unique_bry);
|
||||
bfr.Add_bfr_and_clear(tmp_bfr);
|
||||
|
||||
@@ -52,7 +52,7 @@ public class Pfunc_tag extends Pf_func_base {// REF:CoreParserFunctions.php
|
||||
|
||||
// add to UNIQ hash; DATE:2017-03-31
|
||||
byte[] val = tmp_bfr.To_bry_and_clear();
|
||||
byte[] key = ctx.Wiki().Parser_mgr().Uniq_mgr().Add(tag_name, val);
|
||||
byte[] key = ctx.Wiki().Parser_mgr().Uniq_mgr().Add(Bool_.Y, tag_name, val);
|
||||
bfr.Add(key);
|
||||
}
|
||||
finally {tmp_bfr.Mkr_rls();}
|
||||
|
||||
@@ -14,12 +14,18 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
|
||||
import gplx.core.bits.*;
|
||||
import gplx.core.bits.*; import gplx.core.btries.*;
|
||||
import gplx.xowa.langs.msgs.*;
|
||||
import gplx.xowa.xtns.scribunto.procs.*;
|
||||
public class Scrib_lib_text implements Scrib_lib {
|
||||
private final Scrib_lib_text__json_util json_util = new Scrib_lib_text__json_util();
|
||||
public Scrib_lib_text(Scrib_core core) {this.core = core;} private Scrib_core core;
|
||||
private final Scrib_lib_text__nowiki_util nowiki_util = new Scrib_lib_text__nowiki_util();
|
||||
private final Scrib_core core;
|
||||
private final Btrie_slim_mgr trie;
|
||||
public Scrib_lib_text(Scrib_core core) {
|
||||
this.core = core;
|
||||
this.trie = nowiki_util.Make_trie(gplx.xowa.parsers.xndes.Xop_xnde_tag_.Tag__nowiki.Name_bry());
|
||||
}
|
||||
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
|
||||
public Scrib_lib Init() {procs.Init_by_lib(this, Proc_names); return this;}
|
||||
public Scrib_lib Clone_lib(Scrib_core core) {return new Scrib_lib_text(core);}
|
||||
@@ -47,7 +53,11 @@ public class Scrib_lib_text implements Scrib_lib {
|
||||
, Invk_init_text_for_wiki = "init_text_for_wiki", Invk_jsonEncode = "jsonEncode", Invk_jsonDecode = "jsonDecode";
|
||||
private static final String[] Proc_names = String_.Ary(Invk_unstrip, Invk_unstripNoWiki, Invk_killMarkers, Invk_getEntityTable, Invk_init_text_for_wiki, Invk_jsonEncode, Invk_jsonDecode);
|
||||
public boolean Unstrip(Scrib_proc_args args, Scrib_proc_rslt rslt) {return rslt.Init_obj(args.Pull_str(0));} // NOTE: XOWA does not use MediaWiki strip markers; just return original; DATE:2015-01-20
|
||||
public boolean UnstripNoWiki(Scrib_proc_args args, Scrib_proc_rslt rslt) {return rslt.Init_obj(args.Pull_str(0));} // NOTE: XOWA does not use MediaWiki strip markers; just return original; DATE:2015-01-20
|
||||
public boolean UnstripNoWiki(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
// NOTE: XOWA does not use MediaWiki strip markers; just return original; DATE:2015-01-20
|
||||
byte[] src = args.Pull_bry(0);
|
||||
return rslt.Init_obj(nowiki_util.Strip_tag(core.Page().Url_bry_safe(), src, trie));
|
||||
}
|
||||
public boolean KillMarkers(Scrib_proc_args args, Scrib_proc_rslt rslt) {return rslt.Init_obj(args.Pull_str(0));} // NOTE: XOWA does not use MediaWiki strip markers; just return original; DATE:2015-01-20
|
||||
public boolean GetEntityTable(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
if (html_entities == null) html_entities = Scrib_lib_text_html_entities.new_();
|
||||
@@ -108,10 +118,11 @@ public class Scrib_lib_text implements Scrib_lib {
|
||||
if (Bitmask_.Has_int(flags, Scrib_lib_text__json_util.Flag__try_fixing))
|
||||
opts = Bitmask_.Add_int(opts, Scrib_lib_text__json_util.Flag__try_fixing);
|
||||
|
||||
return JsonDecodeStatic(args, rslt, core, json_util, json, opts, flags);
|
||||
Keyval[] rv = JsonDecodeStatic(args, core, json_util, json, opts, flags);
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
public static boolean JsonDecodeStatic
|
||||
( Scrib_proc_args args, Scrib_proc_rslt rslt, Scrib_core core, Scrib_lib_text__json_util json_util
|
||||
public static Keyval[] JsonDecodeStatic
|
||||
( Scrib_proc_args args, Scrib_core core, Scrib_lib_text__json_util json_util
|
||||
, byte[] json, int opts, int flags) {
|
||||
// decode json to Object; note that Bool_.Y means ary and Bool_.N means ary
|
||||
byte rv_tid = json_util.Decode(core.App().Utl__json_parser(), json, opts);
|
||||
@@ -125,10 +136,10 @@ public class Scrib_lib_text implements Scrib_lib {
|
||||
json_util.Reindex_arrays(reindex_data, rv_as_kvy, false);
|
||||
rv_as_kvy = reindex_data.Rv_is_kvy() ? (Keyval[])reindex_data.Rv_as_kvy() : (Keyval[])reindex_data.Rv_as_ary();
|
||||
}
|
||||
return rslt.Init_obj(rv_as_kvy);
|
||||
return rv_as_kvy;
|
||||
}
|
||||
else
|
||||
return rslt.Init_obj(json_util.Decode_rslt_as_ary());
|
||||
return json_util.Decode_rslt_as_ary();
|
||||
}
|
||||
|
||||
public void Notify_wiki_changed() {if (notify_wiki_changed_fnc != null) core.Interpreter().CallFunction(notify_wiki_changed_fnc.Id(), Keyval_.Ary_empty);}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
|
||||
import gplx.core.btries.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Scrib_lib_text__nowiki_util {
|
||||
public Btrie_slim_mgr Make_trie(byte[] tag) {
|
||||
Bry_bfr tmp = Bry_bfr_.New();
|
||||
Btrie_slim_mgr rv = Btrie_slim_mgr.ci_u8();
|
||||
byte[] lhs_bry = tmp.Add_bry_many(Byte_ascii.Angle_bgn_bry, tag, Byte_ascii.Angle_end_bry).To_bry_and_clear();
|
||||
byte[] rhs_bry = tmp.Add_bry_many(Byte_ascii.Angle_bgn_bry, Byte_ascii.Slash_bry, tag, Byte_ascii.Angle_end_bry).To_bry_and_clear();
|
||||
rv.Add_obj(lhs_bry, Bool_obj_val.True);
|
||||
rv.Add_obj(rhs_bry, Bool_obj_val.False);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Strip_tag(byte[] page, byte[] src, Btrie_slim_mgr trie) {
|
||||
Btrie_rv trv = new Btrie_rv();
|
||||
Bry_bfr tmp = null;
|
||||
int bgn = 0;
|
||||
int end = src.length;
|
||||
|
||||
// main loop
|
||||
boolean lhs_found = false;
|
||||
int pos = bgn;
|
||||
int rhs_end = pos, lhs_bgn = pos, lhs_end = pos;
|
||||
while (pos < end) {
|
||||
// check byte against trie
|
||||
Object o = trie.Match_at_w_b0(trv, src[pos], src, pos, end);
|
||||
|
||||
// no match; increment and continue;
|
||||
if (o == null) {
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// match found
|
||||
Bool_obj_val tag_marker = (Bool_obj_val)o;
|
||||
|
||||
// match is open tag; EX: <tag>
|
||||
if (tag_marker.Val()) {
|
||||
// set lhs_bgn and lhs_end; note that if there are multiple open tags, it will only keep the first
|
||||
if (!lhs_found) {
|
||||
lhs_found = true;
|
||||
lhs_bgn = pos;
|
||||
lhs_end = trv.Pos();
|
||||
}
|
||||
}
|
||||
// match is close tag; EX: </tag>
|
||||
else {
|
||||
// only splice if open tag exists; avoids dangling rhs; EX: "a</tag>b"
|
||||
if (lhs_found) {
|
||||
lhs_found = false;
|
||||
if (tmp == null) tmp = Bry_bfr_.New();
|
||||
|
||||
// add text from previous </tag> to current <tag>;
|
||||
tmp.Add_mid(src, rhs_end, lhs_bgn);
|
||||
|
||||
// add text between <tag> and </tag>;
|
||||
tmp.Add_mid(src, lhs_end, pos);
|
||||
|
||||
// update </tag> pos
|
||||
rhs_end = trv.Pos();
|
||||
}
|
||||
}
|
||||
|
||||
// update pos to after match
|
||||
pos = trv.Pos();
|
||||
}
|
||||
|
||||
// add remaining text to bfr
|
||||
if (tmp != null) {
|
||||
tmp.Add_mid(src, rhs_end, end);
|
||||
}
|
||||
|
||||
return tmp == null ? src : tmp.To_bry_and_clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
import gplx.core.btries.*;
|
||||
import gplx.xowa.parsers.xndes.*;
|
||||
public class Scrib_lib_text__nowiki_util_tst {
|
||||
private final Scrib_lib_text__nowiki_util_fxt fxt = new Scrib_lib_text__nowiki_util_fxt();
|
||||
@Test public void Basic() {
|
||||
// noop
|
||||
fxt.Test__Strip_tag("abc", "abc");
|
||||
|
||||
// one
|
||||
fxt.Test__Strip_tag("a<nowiki>b</nowiki>c", "abc");
|
||||
|
||||
// mixed case
|
||||
fxt.Test__Strip_tag("a<NOwiki>b</noWIKI>c", "abc");
|
||||
|
||||
// multiple: consecutive
|
||||
fxt.Test__Strip_tag("a<nowiki>b</nowiki>c<nowiki>d</nowiki>e", "abcde");
|
||||
|
||||
// dangling: left
|
||||
fxt.Test__Strip_tag("a<nowiki>b<nowiki>c</nowiki>d", "ab<nowiki>cd");
|
||||
|
||||
// dangling: right
|
||||
fxt.Test__Strip_tag( "a<nowiki>b</nowiki>c</nowiki>d", "abc</nowiki>d");
|
||||
|
||||
// nested
|
||||
fxt.Test__Strip_tag("a<nowiki>b<nowiki>c</nowiki>d</nowiki>e", "ab<nowiki>cd</nowiki>e");
|
||||
}
|
||||
}
|
||||
class Scrib_lib_text__nowiki_util_fxt {
|
||||
private final Scrib_lib_text__nowiki_util util = new Scrib_lib_text__nowiki_util();
|
||||
private final Btrie_slim_mgr trie;
|
||||
public Scrib_lib_text__nowiki_util_fxt() {
|
||||
this.trie = util.Make_trie(Xop_xnde_tag_.Tag__nowiki.Name_bry());
|
||||
}
|
||||
public void Test__Strip_tag(String src, String expd) {
|
||||
byte[] actl = util.Strip_tag(Bry_.new_a7("Page"), Bry_.new_u8(src), trie);
|
||||
Gftest.Eq__str(expd, actl);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,9 @@ public class Scrib_lib_text_tst {
|
||||
@Test public void Unstrip() {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_text.Invk_unstrip, Object_.Ary("a"), "a");
|
||||
}
|
||||
@Test public void UnstripNoWiki() {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_text.Invk_unstripNoWiki, Object_.Ary("a<nowiki>b</nowiki>c"), "abc");
|
||||
}
|
||||
@Test public void GetEntityTable() {
|
||||
Keyval[] actl = fxt.Test_scrib_proc_rv_as_kv_ary(lib, Scrib_lib_text.Invk_getEntityTable, Object_.Ary());
|
||||
Tfds.Eq(1510, actl.length); // large result; only test # of entries
|
||||
|
||||
Reference in New Issue
Block a user