mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.2.1
This commit is contained in:
129
400_xowa/src/gplx/xowa/xtns/translates/Xop_languages_xnde.java
Normal file
129
400_xowa/src/gplx/xowa/xtns/translates/Xop_languages_xnde.java
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
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.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.primitives.*;
|
||||
import gplx.xowa.html.*; import gplx.xowa.html.hrefs.*; import gplx.xowa.langs.*; import gplx.xowa.wikis.data.tbls.*;
|
||||
public class Xop_languages_xnde implements Xox_xnde {
|
||||
public Xop_xnde_tkn Xnde() {return xnde;} private Xop_xnde_tkn xnde;
|
||||
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
|
||||
this.xnde = xnde;
|
||||
langs = Find_lang_pages(ctx, wiki);
|
||||
}
|
||||
public List_adp Langs() {return langs;} private List_adp langs;
|
||||
public Xoa_ttl Root_ttl() {return root_ttl;} private Xoa_ttl root_ttl;
|
||||
private Xoa_ttl Root_ttl_of(Xowe_wiki wiki, Xoa_ttl ttl) {
|
||||
byte[] page_bry = ttl.Page_db();
|
||||
int slash_pos = Bry_finder.Find_bwd(page_bry, Xoa_ttl.Subpage_spr);
|
||||
if (slash_pos == Bry_.NotFound) return ttl;
|
||||
byte[] root_bry = Bry_.Mid(page_bry, 0, slash_pos);
|
||||
return Xoa_ttl.parse_(wiki, ttl.Ns().Id(), root_bry);
|
||||
}
|
||||
private List_adp Find_lang_pages(Xop_ctx ctx, Xowe_wiki wiki) {
|
||||
this.root_ttl = Root_ttl_of(wiki, ctx.Cur_page().Ttl());
|
||||
List_adp rslts = List_adp_.new_();
|
||||
Int_obj_ref rslt_count = Int_obj_ref.new_(0);
|
||||
Xow_ns page_ns = root_ttl.Ns();
|
||||
wiki.Db_mgr().Load_mgr().Load_ttls_for_all_pages(Cancelable_.Never, rslts, null, null, rslt_count, page_ns, root_ttl.Page_db(), Int_.MaxValue, 0, Int_.MaxValue, true, false);
|
||||
int len = rslt_count.Val();
|
||||
if (len == 0) return List_adp_.Noop; // no lang pages; return;
|
||||
List_adp rv = List_adp_.new_();
|
||||
byte[] root_ttl_bry = root_ttl.Page_db(); // get root_ttl_bry; do not use ns
|
||||
int lang_bgn = root_ttl_bry.length + 1; // lang starts after /; EX: "Page" will have subpage of "Page/fr" and lang_bgn of 5
|
||||
boolean english_needed = true;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xowd_page_itm page = (Xowd_page_itm)rslts.Get_at(i);
|
||||
byte[] page_ttl_bry = page.Ttl_page_db();
|
||||
int page_ttl_bry_len = page_ttl_bry.length;
|
||||
if (Bry_.Eq(root_ttl_bry, page_ttl_bry)) continue; // ignore self; EX: "page"
|
||||
if (lang_bgn < page_ttl_bry_len // guard against out of bounds
|
||||
&& page_ttl_bry[lang_bgn - 1] == Xoa_ttl.Subpage_spr // prv char must be /; EX: "Page/fr"
|
||||
) {
|
||||
byte[] lang_key = Bry_.Mid(page_ttl_bry, lang_bgn, page_ttl_bry_len);
|
||||
if (Bry_.Eq(lang_key, Xol_lang_.Key_en)) // lang is english; mark english found;
|
||||
english_needed = false;
|
||||
Xol_lang_itm lang_itm = Xol_lang_itm_.Get_by_key(lang_key);
|
||||
if (lang_itm == null) continue; // not a known lang
|
||||
rv.Add(lang_itm);
|
||||
}
|
||||
}
|
||||
if (rv.Count() == 0) return List_adp_.Noop; // no lang items; handles situations where just "Page" is returned
|
||||
if (english_needed) // english not found; always add; handles situations wherein Page/fr and Page/de added, but not Page/en
|
||||
rv.Add(Xol_lang_itm_.Get_by_key(Xol_lang_.Key_en));
|
||||
rv.Sort_by(Xol_lang_itm_.Comparer_key);
|
||||
return rv;
|
||||
}
|
||||
public void Xtn_write(Bry_bfr bfr, Xoae_app app, Xop_ctx ctx, Xoh_html_wtr html_wtr, Xoh_wtr_ctx hctx, Xop_xnde_tkn xnde, byte[] src) {
|
||||
if (langs.Count() == 0) return; // no langs; don't write anything;
|
||||
fmtr_mgr_itms.Init(langs, ctx.Wiki(), root_ttl, ctx.Cur_page().Lang().Key_bry());
|
||||
fmtr_all.Bld_bfr_many(bfr, "Other languages", fmtr_mgr_itms);
|
||||
}
|
||||
private static final Xop_languages_fmtr fmtr_mgr_itms = new Xop_languages_fmtr();
|
||||
public static final Bry_fmtr fmtr_all = Bry_fmtr.new_(String_.Concat_lines_nl
|
||||
( "<table>"
|
||||
, " <tbody>"
|
||||
, " <tr valign=\"top\">"
|
||||
, " <td class=\"mw-pt-languages-label\">~{other_languages_hdr}:</td>"
|
||||
, " <td class=\"mw-pt-languages-list\">~{language_itms}"
|
||||
, " </td>"
|
||||
, " </tr>"
|
||||
, " </tbody>"
|
||||
, "</table>"
|
||||
), "other_languages_hdr", "language_itms")
|
||||
, fmtr_itm_basic = Bry_fmtr.new_(String_.Concat_lines_nl_skip_last
|
||||
( ""
|
||||
, " <a href=\"~{anchor_href}\" title=\"~{anchor_title}\">~{anchor_text}</a> •"
|
||||
), "anchor_href", "anchor_title", "anchor_text")
|
||||
, fmtr_itm_english = Bry_fmtr.new_(String_.Concat_lines_nl_skip_last
|
||||
( ""
|
||||
, " <a href=\"~{anchor_href}\" title=\"~{anchor_title}\"><span class=\"mw-pt-languages-ui\">~{anchor_text}</span></a> •"
|
||||
), "anchor_href", "anchor_title", "anchor_text")
|
||||
, fmtr_itm_selected = Bry_fmtr.new_(String_.Concat_lines_nl_skip_last
|
||||
( ""
|
||||
, " <span class=\"mw-pt-languages-selected\">~{anchor_text}</span> •"
|
||||
), "anchor_href", "anchor_title", "anchor_text")
|
||||
;
|
||||
// "<img src=\"//bits.wikimedia.org/static-1.22wmf9/extensions/Translate/res/images/prog-1.png\" alt=\"~{img_alt}\" title=\"~{img_title}\" width=\"9\" height=\"9\" /> • "
|
||||
}
|
||||
class Xop_languages_fmtr implements Bry_fmtr_arg {
|
||||
public void Init(List_adp langs, Xowe_wiki wiki, Xoa_ttl root_ttl, byte[] cur_lang) {
|
||||
this.langs = langs;
|
||||
this.wiki = wiki;
|
||||
this.root_ttl = root_ttl;
|
||||
this.cur_lang = cur_lang;
|
||||
} private List_adp langs; private Xowe_wiki wiki; private Xoa_ttl root_ttl; private byte[] cur_lang;
|
||||
public void XferAry(Bry_bfr bfr, int idx) {
|
||||
int len = langs.Count();
|
||||
Xoh_href_parser parser = wiki.Appe().Href_parser();
|
||||
int ns_id = root_ttl.Ns().Id();
|
||||
byte[] root_ttl_bry = root_ttl.Page_db(); // NOTE: do not use .Full(); ns will be added in Xoa_ttl.parse below
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xol_lang_itm lang = (Xol_lang_itm)langs.Get_at(i);
|
||||
byte[] lang_key = lang.Key();
|
||||
boolean lang_is_en = Bry_.Eq(lang_key, Xol_lang_.Key_en);
|
||||
byte[] lang_ttl_bry = lang_is_en ? root_ttl_bry : Bry_.Add_w_dlm(Xoa_ttl.Subpage_spr, root_ttl_bry, lang_key);
|
||||
Xoa_ttl lang_ttl = Xoa_ttl.parse_(wiki, ns_id, lang_ttl_bry);
|
||||
byte[] lang_href = parser.Build_to_bry(wiki, lang_ttl);
|
||||
byte[] lang_title = Xoh_html_wtr.Ttl_to_title(lang_ttl.Full_txt());
|
||||
Bry_fmtr fmtr = null;
|
||||
if (Bry_.Eq(lang_key, Xol_lang_.Key_en)) fmtr = Xop_languages_xnde.fmtr_itm_english;
|
||||
else if (Bry_.Eq(lang_key, cur_lang)) fmtr = Xop_languages_xnde.fmtr_itm_selected;
|
||||
else fmtr = Xop_languages_xnde.fmtr_itm_basic;
|
||||
fmtr.Bld_bfr_many(bfr, lang_href, lang_title, lang.Localized_name());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Xop_languages_xnde_tst {
|
||||
@Before public void init() {
|
||||
Io_mgr.I.InitEngine_mem();
|
||||
fxt.Reset();
|
||||
fxt.Page_ttl_("Help:A");
|
||||
fxt.Init_page_create("Help:A", ""); // create for AllPages
|
||||
} private Xop_fxt fxt = new Xop_fxt();
|
||||
@Test public void None() {
|
||||
fxt.Test_parse_page_all_str("<languages/>", ""); // empty
|
||||
}
|
||||
@Test public void Many() {
|
||||
fxt.Init_page_create("Help:A/de", "");
|
||||
fxt.Init_page_create("Help:A/en", "");
|
||||
fxt.Init_page_create("Help:A/fr", "");
|
||||
fxt.Test_parse_page_all_str("<languages/>", Many_expd);
|
||||
}
|
||||
@Test public void Many_english_implied() {
|
||||
fxt.Init_page_create("Help:A/de", "");
|
||||
fxt.Init_page_create("Help:A/fr", "");
|
||||
fxt.Test_parse_page_all_str("<languages/>", Many_expd);
|
||||
}
|
||||
@Test public void Current_is_french() {
|
||||
fxt.Init_page_create("Help:A/de", "");
|
||||
fxt.Init_page_create("Help:A/fr", "");
|
||||
fxt.Page_ttl_("Help:A/fr");
|
||||
fxt.Test_parse_page_all_str("<languages/>", String_.Concat_lines_nl
|
||||
( "<table>"
|
||||
, " <tbody>"
|
||||
, " <tr valign=\"top\">"
|
||||
, " <td class=\"mw-pt-languages-label\">Other languages:</td>"
|
||||
, " <td class=\"mw-pt-languages-list\">"
|
||||
, " <a href=\"/wiki/Help:A/de\" title=\"Help:A/de\">Deutsch</a> •"
|
||||
, " <a href=\"/wiki/Help:A\" title=\"Help:A\"><span class=\"mw-pt-languages-ui\">English</span></a> •"
|
||||
, " <a href=\"/wiki/Help:A/fr\" title=\"Help:A/fr\">Français</a> •"
|
||||
, " </td>"
|
||||
, " </tr>"
|
||||
, " </tbody>"
|
||||
, "</table>"
|
||||
));
|
||||
}
|
||||
private static final String Many_expd = String_.Concat_lines_nl
|
||||
( "<table>"
|
||||
, " <tbody>"
|
||||
, " <tr valign=\"top\">"
|
||||
, " <td class=\"mw-pt-languages-label\">Other languages:</td>"
|
||||
, " <td class=\"mw-pt-languages-list\">"
|
||||
, " <a href=\"/wiki/Help:A/de\" title=\"Help:A/de\">Deutsch</a> •"
|
||||
, " <a href=\"/wiki/Help:A\" title=\"Help:A\"><span class=\"mw-pt-languages-ui\">English</span></a> •"
|
||||
, " <a href=\"/wiki/Help:A/fr\" title=\"Help:A/fr\">Français</a> •"
|
||||
, " </td>"
|
||||
, " </tr>"
|
||||
, " </tbody>"
|
||||
, "</table>"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.xowa.specials.*;
|
||||
public class Xop_mylanguage_page implements Xows_page {
|
||||
public Xows_special_meta Special_meta() {return Xows_special_meta_.Itm__my_language;}
|
||||
public void Special_gen(Xowe_wiki wiki, Xoae_page page, Xoa_url url, Xoa_ttl ttl) {
|
||||
// Special:MyLanguage/Help:A -> Help:A/fr
|
||||
byte[] page_bry = ttl.Leaf_txt_wo_qarg(); // EX: Help:A
|
||||
byte[] lang_key = wiki.Appe().Usere().Lang().Key_bry(); // EX: fr
|
||||
byte[] trg_bry = page_bry;
|
||||
boolean lang_is_english = Bry_.Eq(lang_key, Xol_lang_.Key_en);
|
||||
if (!lang_is_english)
|
||||
trg_bry = Bry_.Add_w_dlm(Xoa_ttl.Subpage_spr, page_bry, lang_key);
|
||||
Xoae_page found_page = wiki.Data_mgr().Redirect(page, trg_bry);
|
||||
if (found_page.Missing() && !lang_is_english) // foreign lang does not exist; default to english
|
||||
wiki.Data_mgr().Redirect(page, page_bry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Xop_mylanguage_page_tst {
|
||||
@Before public void init() {fxt.Clear();} private Xop_mylanguage_page_fxt fxt = new Xop_mylanguage_page_fxt();
|
||||
@Test public void Non_english_exists() {
|
||||
fxt.Init_create_page("Help:A/fr");
|
||||
fxt.Init_cur_lang("fr");
|
||||
fxt.Test_open("Special:MyLanguage/Help:A", "Help:A/fr");
|
||||
}
|
||||
@Test public void English() {
|
||||
fxt.Init_create_page("Help:A");
|
||||
fxt.Init_cur_lang("en");
|
||||
fxt.Test_open("Special:MyLanguage/Help:A", "Help:A");
|
||||
}
|
||||
@Test public void Non_english_absent() {
|
||||
fxt.Init_create_page("Help:A");
|
||||
fxt.Init_cur_lang("fr");
|
||||
fxt.Test_open("Special:MyLanguage/Help:A", "Help:A");
|
||||
}
|
||||
}
|
||||
class Xop_mylanguage_page_fxt {
|
||||
public void Clear() {
|
||||
parser_fxt = new Xop_fxt();
|
||||
parser_fxt.Reset();
|
||||
app = parser_fxt.App();
|
||||
wiki = parser_fxt.Wiki();
|
||||
special_page = wiki.Special_mgr().Page_mylanguage();
|
||||
} private Xop_fxt parser_fxt; private Xoae_app app; private Xop_mylanguage_page special_page; private Xowe_wiki wiki;
|
||||
public void Init_create_page(String page) {parser_fxt.Init_page_create(page, page);}
|
||||
public void Init_cur_lang(String lang) {app.Sys_cfg().Lang_(Bry_.new_a7(lang));}
|
||||
public void Test_open(String link, String expd) {
|
||||
Xoae_page page = parser_fxt.Ctx().Cur_page();
|
||||
Xoa_url url = app.Utl__url_parser().Parse(Bry_.new_u8(link));
|
||||
page.Url_(url);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, Bry_.new_a7(link));
|
||||
page.Ttl_(ttl);
|
||||
special_page.Special_gen(wiki, page, url, ttl);
|
||||
Tfds.Eq(expd, String_.new_a7(page.Url().Page_bry()));
|
||||
Tfds.Eq(expd, String_.new_a7(page.Data_raw()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
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.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.xowa.html.*;
|
||||
public class Xop_translate_xnde implements Xox_xnde, Xop_xnde_atr_parser {
|
||||
public Xop_root_tkn Xtn_root() {return xtn_root;} private Xop_root_tkn xtn_root;
|
||||
public void Xatr_parse(Xowe_wiki wiki, byte[] src, Xop_xatr_itm xatr, Object xatr_obj) {}
|
||||
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
|
||||
byte[] translate_src = Bry_.Mid(src, xnde.Tag_open_end(), xnde.Tag_close_bgn());
|
||||
translate_src = Bry_.Trim(translate_src, 0, translate_src.length);
|
||||
Xop_ctx sub_ctx = Xop_ctx.new_sub_(wiki);
|
||||
xtn_root = wiki.Parser().Parse_text_to_wdom_old_ctx(sub_ctx, translate_src, true);
|
||||
}
|
||||
public void Xtn_write(Bry_bfr bfr, Xoae_app app, Xop_ctx ctx, Xoh_html_wtr html_wtr, Xoh_wtr_ctx hctx, Xop_xnde_tkn xnde, byte[] src) {
|
||||
html_wtr.Write_tkn(bfr, ctx, hctx, xtn_root.Root_src(), xnde, Xoh_html_wtr.Sub_idx_null, xtn_root);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
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.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Xop_translate_xnde_tst {
|
||||
@Before public void init() {fxt.Reset();} private Xop_fxt fxt = new Xop_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test_parse_page_all_str("<translate> ''a'' </translate>", "<i>a</i>"); // NOTE: trim ws
|
||||
}
|
||||
@Test public void Header() {
|
||||
fxt.Test_parse_page_all_str("<translate>==a==</translate>", "<h2>a</h2>\n");
|
||||
}
|
||||
}
|
||||
37
400_xowa/src/gplx/xowa/xtns/translates/Xop_tvar_lxr.java
Normal file
37
400_xowa/src/gplx/xowa/xtns/translates/Xop_tvar_lxr.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.btries.*;
|
||||
public class Xop_tvar_lxr implements Xop_lxr {
|
||||
public byte Lxr_tid() {return Xop_lxr_.Tid_tvar;}
|
||||
public void Init_by_wiki(Xowe_wiki wiki, Btrie_fast_mgr core_trie) {core_trie.Add(Hook_bgn, this);}
|
||||
public void Init_by_lang(Xol_lang lang, Btrie_fast_mgr core_trie) {}
|
||||
private static final byte[] Hook_bgn = Bry_.new_a7("<tvar|"), Close_nde = Bry_.new_a7("</>");
|
||||
public int Make_tkn(Xop_ctx ctx, Xop_tkn_mkr tkn_mkr, Xop_root_tkn root, byte[] src, int src_len, int bgn_pos, int cur_pos) {
|
||||
int rhs_end = Bry_finder.Find_fwd(src, Byte_ascii.Gt, cur_pos); if (rhs_end == Bry_.NotFound) return ctx.Lxr_make_txt_(cur_pos);
|
||||
int lhs_bgn = Bry_finder.Find_fwd(src, Close_nde , rhs_end); if (lhs_bgn == Bry_.NotFound) return ctx.Lxr_make_txt_(cur_pos);
|
||||
byte[] body = Bry_.Mid(src, rhs_end + Int_.Const_position_after_char, lhs_bgn);
|
||||
Xop_ctx sub_ctx = Xop_ctx.new_sub_(ctx.Wiki());
|
||||
Xop_root_tkn sub_root = tkn_mkr.Root(body);
|
||||
body = ctx.Wiki().Parser().Parse_text_to_wtxt(sub_root, sub_ctx, tkn_mkr, body); // NOTE: must parse inner text for templates; EX:<tvar|a>{{B}}</>; PAGE:mw:Download; DATE:2014-04-27
|
||||
int end_pos = lhs_bgn + Close_nde.length;
|
||||
ctx.Subs_add(root, tkn_mkr.Tvar(bgn_pos, end_pos, cur_pos, rhs_end, rhs_end + Int_.Const_position_after_char, lhs_bgn, body));
|
||||
return end_pos;
|
||||
}
|
||||
public static final Xop_tvar_lxr _ = new Xop_tvar_lxr(); Xop_tvar_lxr() {}
|
||||
}
|
||||
32
400_xowa/src/gplx/xowa/xtns/translates/Xop_tvar_lxr_tst.java
Normal file
32
400_xowa/src/gplx/xowa/xtns/translates/Xop_tvar_lxr_tst.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
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.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Xop_tvar_lxr_tst {
|
||||
@Before public void init() {fxt.Reset();} private Xop_fxt fxt = new Xop_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test_parse_page_all_str("<tvar|1>''a''</>", "<i>a</i>");
|
||||
}
|
||||
@Test public void Missing_end() {
|
||||
fxt.Test_parse_page_all_str("<tvar|1>''a''</tvar>", "<tvar|1><i>a</i></tvar>");
|
||||
}
|
||||
@Test public void Templates() {
|
||||
fxt.Init_defn_add("A", "a");
|
||||
fxt.Test_parse_page_all_str("<tvar|1>{{A}}</>", "a");
|
||||
}
|
||||
}
|
||||
32
400_xowa/src/gplx/xowa/xtns/translates/Xop_tvar_tkn.java
Normal file
32
400_xowa/src/gplx/xowa/xtns/translates/Xop_tvar_tkn.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
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.xtns.translates; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
public class Xop_tvar_tkn extends Xop_tkn_itm_base {
|
||||
public Xop_tvar_tkn(int tkn_bgn, int tkn_end, int key_bgn, int key_end, int txt_bgn, int txt_end, byte[] wikitext) {
|
||||
this.Tkn_ini_pos(false, tkn_bgn, tkn_end);
|
||||
this.key_bgn = key_bgn; this.key_end = key_end;
|
||||
this.txt_bgn = txt_bgn; this.txt_end = txt_end;
|
||||
this.wikitext = wikitext;
|
||||
}
|
||||
@Override public byte Tkn_tid() {return Xop_tkn_itm_.Tid_tvar;}
|
||||
public int Key_bgn() {return key_bgn;} private int key_bgn;
|
||||
public int Key_end() {return key_end;} private int key_end;
|
||||
public int Txt_bgn() {return txt_bgn;} private int txt_bgn;
|
||||
public int Txt_end() {return txt_end;} private int txt_end;
|
||||
public byte[] Wikitext() {return wikitext;} private byte[] wikitext;
|
||||
}
|
||||
Reference in New Issue
Block a user