1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Source: Restore broken commit

This commit is contained in:
gnosygnu
2017-02-06 22:14:55 -05:00
parent 938beac9f9
commit 3bfeb94b43
4380 changed files with 328018 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.xtns.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.primitives.*;
public class Pgbnr_cfg {
private final Hash_adp ns_hash = Hash_adp_.New(); private final Int_obj_ref tmp_ns_key = Int_obj_ref.New_neg1();
public Pgbnr_cfg(boolean enabled, boolean enable_heading_override, boolean enable_default_banner, int[] ns_ary, int dflt_img_wdata_prop, byte[] dflt_img_title, int[] standard_sizes) {
this.enabled = enabled; this.enable_heading_override = enable_heading_override; this.enable_default_banner = enable_default_banner;
this.standard_sizes = standard_sizes;
this.dflt_img_wdata_prop = dflt_img_wdata_prop; this.dflt_img_title = dflt_img_title;
for (int ns : ns_ary)
this.ns_hash.Add_as_key_and_val(Int_obj_ref.New(ns));
}
public final boolean enabled;
public final boolean enable_heading_override;
public final boolean enable_default_banner;
public final int dflt_img_wdata_prop;
public final byte[] dflt_img_title;
public final int[] standard_sizes;
public boolean Chk_pgbnr_allowed(Xoa_ttl ttl, Xowe_wiki wiki) {
boolean enabled_in_ns = ns_hash.Has(tmp_ns_key.Val_(ttl.Ns().Id()));
return enabled_in_ns // chk if ns allows banner
&& !Bry_.Eq(ttl.Page_db(), wiki.Props().Main_page()); // never show on main page
}
}

View File

@@ -0,0 +1,209 @@
/*
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.btries.*; import gplx.langs.mustaches.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.tmpls.*; import gplx.xowa.xtns.pfuncs.*; import gplx.xowa.langs.kwds.*; import gplx.xowa.parsers.utils.*; import gplx.xowa.parsers.lnkis.*; import gplx.xowa.parsers.lnkis.files.*;
import gplx.xowa.files.*;
import gplx.xowa.htmls.core.htmls.*; import gplx.langs.htmls.encoders.*;
public class Pgbnr_func extends Pf_func_base {
@Override public int Id() {return Xol_kwd_grp_.Id_pagebanner;}
@Override public Pf_func New(int id, byte[] name) {return new Pgbnr_func().Name_(name);}
@Override public void Func_evaluate(Bry_bfr bfr, Xop_ctx ctx, Xot_invk caller, Xot_invk self, byte[] src) { // {{PAGEBANNER}} appears on page; WikidataPageBanner.hooks.php|addCustomBanner
Xowe_wiki wiki = ctx.Wiki(); Xoae_page page = ctx.Page();
Pgbnr_xtn_mgr xtn_mgr = wiki.Xtn_mgr().Xtn_pgbnr();
Pgbnr_cfg cfg = xtn_mgr.Cfg();
Xoa_ttl ttl = page.Ttl();
if (!cfg.Chk_pgbnr_allowed(ttl, wiki)) return;
byte[] tooltip = ttl.Page_txt(), title = ttl.Page_txt(), toc = Bry_.Empty, origin_x = Bry_.Empty;
boolean bottomtoc = false;;
double data_pos_x = 0, data_pos_y = 0;
List_adp icons_list = null;
Bry_bfr tmp_bfr = Bry_bfr_.New();
int args_len = self.Args_len();
Xop_func_arg_itm func_arg = new Xop_func_arg_itm();
for (int i = 0; i < args_len; ++i) {
Arg_nde_tkn arg = self.Args_get_by_idx(i);
func_arg.Set(tmp_bfr, ctx, src, caller, self, arg);
byte[] key = func_arg.key;
byte[] val = func_arg.val;
if (key == Bry_.Empty) continue; // ignore blank args; EX:{{PAGEBANNER:A.png|\n|toc=yes}}
int tid = arg_hash.Get_as_int_or(key, -1);
if (tid == Arg__pgname)
tooltip = title = val;
if (tid == Arg__tooltip) // note that this overrides pgname above
tooltip = val;
if (tid == Arg__bottomtoc && Bry_.Eq(val, Bry__yes))
bottomtoc = true;
if (tid == Arg__toc && Bry_.Eq(val, Bry__yes)) // REF.MW:addToc
toc = Bry_.Empty; // note that "" will be overwritten later by actual toc html
if ( tid == -1 // note that "icon-*" won't have a tid
&& Bry_.Has_at_bgn(key, Bry__icon) // if (substr($key, 0, 5) === 'icon-')
&& Bry_.Len(key) > 5 // if ( !isset( $iconname) )
&& Bry_.Len_gt_0(val) // if ( !isset( $$value ) )
) { // REF.MW:addIcons
tid = Arg__icon;
if (icons_list == null) icons_list = List_adp_.New();
byte[] icon_key = Bry_.Mid(key, 5);
byte[] icon_name = Xop_sanitizer.Escape_cls(icon_key);
byte[] icon_title = icon_name;
Xoa_ttl icon_ttl = wiki.Ttl_parse(val);
byte[] icon_href = Bry__icon_href_dflt;
if (icon_ttl != null) {
icon_href = Bry_.Add(gplx.xowa.htmls.hrefs.Xoh_href_.Bry__wiki, icon_ttl.Page_db());
icon_title = icon_ttl.Page_txt();
}
icons_list.Add(new Pgbnr_icon(tmp_bfr, icon_name, icon_title, icon_href));
}
if (tid == Arg__origin) { // REF.MW:addFocus
double tmp_data_pos_x = Double_.NaN, tmp_data_pos_y = Double_.NaN;
int comma_pos = Bry_find_.Find_fwd(val, Byte_ascii.Comma);
if (comma_pos != Bry_find_.Not_found) {
tmp_data_pos_x = Bry_.To_double_or(val, 0, comma_pos, Double_.NaN);
if (!Double_.IsNaN(tmp_data_pos_x)) {
if (tmp_data_pos_x >= -1 && tmp_data_pos_x <= 1) {
data_pos_x = tmp_data_pos_x;
origin_x = tmp_data_pos_x <= .25d ? Bry__origin_x__left : Bry__origin_x__right;
}
}
if (!Double_.IsNaN(tmp_data_pos_y)) {
if (tmp_data_pos_y >= -1 && tmp_data_pos_y <= 1)
data_pos_y = tmp_data_pos_y;
}
}
}
if (tid == -1) Gfo_usr_dlg_.Instance.Warn_many("", "", "unknown arg type; page=~{0} key=~{1} val=~{2}", page.Ttl().Full_db(), key, val);
}
byte[] banner_name = Eval_argx(ctx, src, caller, self);
Xoa_ttl banner_ttl = wiki.Ttl_parse(banner_name); // NOTE: MW also creates title to auto-register page and image in imagelinks
if (banner_ttl == null) // if ttl is invalid, get it from wikidata; PAGE:en.v:Diving_the_Cape_Peninsula_and_False_Bay/Whale_Rock; DATE:2016-07-12
banner_ttl = Get_wikidata_banner(wiki.Appe(), wiki, wiki.Xtn_mgr().Xtn_pgbnr().Cfg(), page.Ttl());
if (banner_ttl == null) { // if ttl is still invalid, exit now else will fail with nullref below; PAGE:en.v:Peterborough (New Hampshire); DATE:2016-07-12
Gfo_usr_dlg_.Instance.Warn_many("", "", "banner file is invalid; page=~{0} banner=~{1}", page.Url_bry_safe(), banner_name);
return;
}
Xof_file_itm banner_file_itm = File__make_tkn(ctx, Xop_file_logger_.Tid__pgbnr_main, banner_ttl, Xop_lnki_tkn.Width_null, Xop_lnki_tkn.Height_null);
Pgbnr_itm itm = new Pgbnr_itm();
itm.Init_from_wtxt(banner_ttl, banner_file_itm, tooltip, title, bottomtoc, toc, data_pos_x, data_pos_y, origin_x, icons_list == null ? Pgbnr_icon.Ary_empty : (Pgbnr_icon[])icons_list.To_ary_and_clear(Pgbnr_icon.class));
page.Html_data().Xtn_pgbnr_(itm);
page.Html_data().Head_mgr().Itm__pgbnr().Enabled_y_(); // register css / js during parse stage
page.Wtxt().Toc().Flag__toc_y_(); // NOTE: must mark toc_manual else will show 2nd TOC in edit mode; DATE:2016-07-10
}
public static void Add_banner(Bry_bfr bfr, Xoae_page wpg, Xop_ctx ctx, Xoh_wtr_ctx hctx, Pgbnr_itm itm) {
Xowe_wiki wiki = ctx.Wiki(); Xoae_app app = wiki.Appe();
Pgbnr_cfg cfg = wiki.Xtn_mgr().Xtn_pgbnr().Cfg(); if (!cfg.enabled) return;
Xoa_ttl ttl = wpg.Ttl();
Xoa_ttl banner_ttl = null; byte[] banner_html = null;
if (itm != null) { // {{PAGEBANNER}} exists in wikitext
itm.Init_hdump(hctx.Mode_is_hdump());
banner_ttl = itm.banner_ttl;
banner_html = Get_banner_html(wiki, ctx, hctx, cfg, banner_ttl, itm);
if (banner_html == null) { // no banner; try again using title from wikidata; note that this should only happen if no banner_ttl or banner_ttl is invalid; EX:{{PAGEBANNER:|toc=yes}}
banner_ttl = Get_wikidata_banner(app, wiki, cfg, ttl);
banner_html = Get_banner_html(wiki, ctx, hctx, cfg, banner_ttl, itm);
}
}
else if ( ttl.Ns().Id_is_main() // {{PAGEBANNER}} missing, but wiki is marked as enable_default_banner
&& cfg.enable_default_banner
&& cfg.Chk_pgbnr_allowed(ttl, wiki)
) {
banner_ttl = Get_wikidata_banner(app, wiki, cfg, ttl);
if (banner_ttl == null)
banner_ttl = wiki.Ttl_parse(cfg.dflt_img_title);
Xof_file_itm banner_file_itm = File__make_tkn(ctx, Xop_file_logger_.Tid__pgbnr_main, banner_ttl, Xop_lnki_tkn.Width_null, Xop_lnki_tkn.Height_null);
itm = new Pgbnr_itm();
itm.Init_from_wtxt(banner_ttl, banner_file_itm, Bry_.Empty, Bry_.Empty, false, Bry_.Empty, 0, 0, Bry_.Empty, Pgbnr_icon.Ary_empty);
itm.Init_hdump(hctx.Mode_is_hdump());
banner_html = Get_banner_html(wiki, ctx, hctx, cfg, banner_ttl, itm);
}
if (banner_html != null)
bfr.Add(banner_html);
}
public static byte[] Get_banner_html(Xowe_wiki wiki, Xop_ctx ctx, Xoh_wtr_ctx hctx, Pgbnr_cfg cfg, Xoa_ttl banner_ttl, Pgbnr_itm itm) {
byte[][] urls = Get_standard_size_urls(wiki, cfg, banner_ttl); if (urls == null) return null;
Bry_bfr tmp_bfr = Bry_bfr_.New();
int urls_len = urls.length;
int[] sizes = cfg.standard_sizes;
for (int i = 0; i < urls_len; ++i) {
int size = sizes[i];
if (i != 0) tmp_bfr.Add_byte_comma();
tmp_bfr.Add(urls[i]).Add_byte_space().Add_int_variable(size).Add_byte(Byte_ascii.Ltr_w); // REF.MW: $srcset[] = "$url {$size}w";
}
byte[] srcset = tmp_bfr.To_bry_and_clear();
byte[] banner_url = itm.banner_img_src != null ? itm.banner_img_src : urls.length == 0 ? Bry_.Empty : urls[urls_len - 1]; // gets largest url
int max_width = itm.banner_file_itm.Orig_w(); // $file = wfFindFile( banner_file ); $options['max_width'] = $file->getWidth();
byte[] banner_file = null; // $bannerfile->getLocalUrl();
byte[] toc_html = null;
if (hctx.Mode_is_hdump()) {
gplx.xowa.htmls.core.wkrs.tocs.Xoh_toc_wtr.Write_tag(tmp_bfr, true);
toc_html = tmp_bfr.To_bry_and_clear();
banner_file = Bry_.Add(gplx.xowa.htmls.hrefs.Xoh_href_.Bry__wiki, gplx.xowa.wikis.nss.Xow_ns_.Bry__file, Byte_ascii.Colon_bry
, Gfo_url_encoder_.Href.Encode(banner_ttl.Full_db())); // NOTE: must encode so "'" becomes "%27", not "&#39;"; PAGE:en.v:'s-Hertogenbosch; DATE:2016-07-12
}
else {
ctx.Page().Html_data().Toc_mgr().To_html(tmp_bfr, Xoh_wtr_ctx.Basic, true);
toc_html = tmp_bfr.To_bry_and_clear();
}
itm.Init_from_html(max_width, banner_file, banner_url, srcset, cfg.enable_heading_override, toc_html);
Mustache_render_ctx mctx = new Mustache_render_ctx().Init(itm);
Mustache_bfr mbfr = Mustache_bfr.New_bfr(tmp_bfr);
wiki.Xtn_mgr().Xtn_pgbnr().Template_root().Render(mbfr, mctx);
return mbfr.To_bry_and_clear();
}
private static byte[][] Get_standard_size_urls(Xow_wiki wiki, Pgbnr_cfg cfg, Xoa_ttl banner_ttl) {
Ordered_hash hash = Ordered_hash_.New_bry();
int[] sizes = cfg.standard_sizes;
int sizes_len = sizes.length;
for (int i = 0; i < sizes_len; ++i) {
byte[] url = Get_image_url(wiki, banner_ttl, sizes[i]);
if (url != null)
hash.Add_if_dupe_use_1st(url, url);
}
return (byte[][])hash.To_ary_and_clear(byte[].class);
}
private static byte[] Get_image_url(Xow_wiki wiki, Xoa_ttl banner_ttl, int width) {
// Object file = new Object(); // $file = wfFindFile( file_ttl );
// if (file == null) return null;
if (width >= 0 && width <= 3000) {
// $mto = $file->transform( array( 'width' => $imagewidth ) );
byte[] url = new byte[0]; // $url = wfExpandUrl( $mto->getUrl(), PROTO_CURRENT );
return url;
}
else
return new byte[0]; // $file->getFullUrl();
}
private static Xoa_ttl Get_wikidata_banner(Xoae_app app, Xow_wiki wiki, Pgbnr_cfg cfg, Xoa_ttl ttl) {
byte[] rv = app.Wiki_mgr().Wdata_mgr().Get_claim_or(wiki.Domain_itm(), ttl, cfg.dflt_img_wdata_prop, null); // don't log misses; wikivoyage pages will default to show pagebanner, and many pages may not have wikidata definitions
if (rv == null) return null;
return wiki.Ttl_parse(rv);
}
private static Xof_file_itm File__make_tkn(Xop_ctx ctx, byte tid, Xoa_ttl file_ttl, int file_w, int file_h) {
Xop_lnki_tkn lnki = ctx.Tkn_mkr().Lnki(file_w, file_h).Ttl_(file_ttl);
ctx.Page().Lnki_list().Add(lnki);
ctx.Lnki().File_logger().Log_file(ctx, lnki, tid); // NOTE: do not set file_wkr ref early (as member var); parse_all sets late
Xof_file_itm file_itm = ctx.Wiki().Html_mgr().Html_wtr().Lnki_wtr().File_wtr().Lnki_eval(Xof_exec_tid.Tid_wiki_page, ctx, ctx.Page(), lnki);
return file_itm;
}
private static final byte[] Bry__yes = Bry_.new_a7("yes"), Bry__icon = Bry_.new_a7("icon-"), Bry__icon_href_dflt = Bry_.new_a7("#"), Bry__origin_x__left = Bry_.new_a7("wpb-left"), Bry__origin_x__right = Bry_.new_a7("wpb-right");
private static final int Arg__pgname = 0, Arg__tooltip = 1, Arg__bottomtoc = 2, Arg__toc = 3, Arg__icon = 4, Arg__origin = 5;
private static final Hash_adp_bry arg_hash = Hash_adp_bry.cs().Add_str_int("pgname", Arg__pgname)
.Add_str_int("tooltip", Arg__tooltip).Add_str_int("bottomtoc", Arg__bottomtoc).Add_str_int("toc", Arg__toc).Add_str_int("origin", Arg__origin);
}

View File

@@ -0,0 +1,86 @@
/*
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*; import gplx.core.brys.*; import gplx.xowa.wikis.pages.skins.*;
import gplx.xowa.htmls.core.htmls.*;
public class Pgbnr_func_tst {
private final Pgbnr_func_fxt fxt = new Pgbnr_func_fxt();
@Test public void Basic() {
fxt.Test__parse(Bool_.N, "{{PAGEBANNER:A.png|icon-star=Star_article}}", String_.Concat_lines_nl_apos_skip_last
( "<div class='ext-wpb-pagebanner noprint pre-content'>"
, " <div class='wpb-topbanner'>"
, " <h1 class='wpb-name'>Test page</h1>"
, " <a href='' class='image' title='Test page' xowa_title='A.png'><img id='xoimg_0' src='file:///mem/wiki/repo/trg/orig/7/0/A.png' width='0' height='0' class='wpb-banner-image ' alt='' srcset='' data-pos-x='0' data-pos-y='0' style='max-width:0px'></a>"
, " <div class='wpb-iconbox'>"
, " <a href='/wiki/Star_article'><span aria-disabled='false' title='Star article' class='oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-star oo-ui-iconElement oo-ui-iconWidget'></span></a>"
, " </div>"
, " </div>"
, " <div class='wpb-topbanner-toc'><div class='wpb-banner-toc'><div>"
, " <div id=\"toctitle\">"
, " <h2>Contents</h2>"
, " </div>"
, "</div>"
, "</div></div>"
, "</div>"
));
}
@Test public void Hdump__basic() {
fxt.Test__parse(Bool_.Y, "{{PAGEBANNER:A.png|icon-star=Star_article}}", String_.Concat_lines_nl_apos_skip_last
( "<div class='ext-wpb-pagebanner noprint pre-content'>"
, " <div class='wpb-topbanner'>"
, " <h1 class='wpb-name'>Test page</h1>"
, " <a href='/wiki/File:A.png' class='image' title='Test page' xowa_title='A.png'><img data-xowa-title=\"A.png\" data-xoimg=\"1|-1|-1|-1|-1|-1\" src='' width='0' height='0' class='wpb-banner-image ' alt='' srcset='' data-pos-x='0' data-pos-y='0' style='max-width:0px'></a>"
, " <div class='wpb-iconbox'>"
, " <a href='/wiki/Star_article'><span aria-disabled='false' title='Star article' class='oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-star oo-ui-iconElement oo-ui-iconWidget'></span></a>"
, " </div>"
, " </div>"
, " <div class='wpb-topbanner-toc'><div class='wpb-banner-toc'><div class=\"xo-toc\" data-toc-mode=\"1\"></div></div></div>"
, "</div>"
));
}
@Test public void Hdump__quote() { // PAGE:en.v:Europe; DATE:2016-07-12
fxt.Test__parse(Bool_.Y, "{{PAGEBANNER:A\"b.png|icon-star=Star_article}}", String_.Concat_lines_nl_apos_skip_last
( "<div class='ext-wpb-pagebanner noprint pre-content'>"
, " <div class='wpb-topbanner'>"
, " <h1 class='wpb-name'>Test page</h1>"
, " <a href='/wiki/File:A%22b.png' class='image' title='Test page' xowa_title='A%22b.png'><img data-xowa-title=\"A%22b.png\" data-xoimg=\"1|-1|-1|-1|-1|-1\" src='' width='0' height='0' class='wpb-banner-image ' alt='' srcset='' data-pos-x='0' data-pos-y='0' style='max-width:0px'></a>"
, " <div class='wpb-iconbox'>"
, " <a href='/wiki/Star_article'><span aria-disabled='false' title='Star article' class='oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-star oo-ui-iconElement oo-ui-iconWidget'></span></a>"
, " </div>"
, " </div>"
, " <div class='wpb-topbanner-toc'><div class='wpb-banner-toc'><div class=\"xo-toc\" data-toc-mode=\"1\"></div></div></div>"
, "</div>"
));
}
}
class Pgbnr_func_fxt {
private final Xop_fxt fxt;
public Pgbnr_func_fxt() {
Xoae_app app = Xoa_app_fxt.Make__app__edit();
Xowe_wiki wiki = Xoa_app_fxt.Make__wiki__edit(app, "en.wikivoyage.org");
fxt = new Xop_fxt(app, wiki);
}
public void Test__parse(boolean hdump, String raw, String expd) {
fxt.Exec_parse_page_all_as_str(raw);
Xoh_wtr_ctx hctx = hdump ? Xoh_wtr_ctx.Hdump : Xoh_wtr_ctx.Basic;
Bfr_arg arg = fxt.Wiki().Xtn_mgr().Xtn_pgbnr().Write_html(fxt.Page(), fxt.Ctx(), hctx);
Bry_bfr bfr = Bry_bfr_.New();
arg.Bfr_arg__add(bfr);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
}

View File

@@ -0,0 +1,42 @@
/*
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.brys.fmtrs.*;
import gplx.langs.mustaches.*;
public class Pgbnr_icon implements Mustache_doc_itm {
private final byte[] name, title, href, html;
public Pgbnr_icon(Bry_bfr tmp_bfr, byte[] name, byte[] title, byte[] href) {
this.name = name; this.title = title; this.href = href;
fmt.Bld_many(tmp_bfr, name, title);
this.html = tmp_bfr.To_bry_and_clear();
}
public boolean Mustache__write(String key, Mustache_bfr bfr) {
if (String_.Eq(key, "name")) bfr.Add_bry(name);
else if (String_.Eq(key, "title")) bfr.Add_bry(title);
else if (String_.Eq(key, "url")) bfr.Add_bry(href);
else if (String_.Eq(key, "html")) bfr.Add_bry(html);
else return false;
return true;
}
public Mustache_doc_itm[] Mustache__subs(String key) {return Mustache_doc_itm_.Ary__empty;}
public static final Pgbnr_icon[] Ary_empty = new Pgbnr_icon[0];
public static final Bry_fmt fmt = Bry_fmt.New
( Bry_.New_u8_nl_apos("<span aria-disabled='false' title='~{title}' class='oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-~{name} oo-ui-iconElement oo-ui-iconWidget'></span>")
, "name", "title"
);
}

View File

@@ -0,0 +1,123 @@
/*
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.langs.mustaches.*; import gplx.xowa.parsers.lnkis.*;
import gplx.xowa.files.*;
import gplx.langs.htmls.*; import gplx.langs.htmls.docs.*; import gplx.xowa.htmls.core.wkrs.imgs.atrs.*; import gplx.xowa.htmls.core.wkrs.lnkis.htmls.*; import gplx.langs.htmls.encoders.*;
public class Pgbnr_itm implements Mustache_doc_itm {
public Xoa_ttl banner_ttl;
public byte[] banner_img_src;
public byte[] toc;
public Xof_file_itm banner_file_itm;
private byte[] banner_anch_title, banner_hdr_text, originx, banner_anch_href, srcset;
private double data_pos_x, data_pos_y;
private int max_width;
private boolean bottomtoc, isHeadingOverrideEnabled;
private byte[] file_ttl;
private Pgbnr_icon[] icons;
private byte[] img_id_atr, img_xottl_atr, img_xoimg_atr;
// NOTE: used by hdump
public boolean Exists() {return exists;} private boolean exists;
public byte[] Srcset() {return srcset;}
public byte[] Style_if_not_dflt() {return Bry_.Eq(style, Atr_val__style_dflt) ? Bry_.Empty : style;}
public byte[] Style() {return style;} private byte[] style;
public double Data_pos_x() {return data_pos_x;}
public double Data_pos_y() {return data_pos_y;}
public void Clear_by_hdump() {
this.exists = false;
this.srcset = style = null;
this.data_pos_x = this.data_pos_y = 0;
}
public void Init_by_parse(Gfh_tag tag) {
this.exists = true;
this.srcset = tag.Atrs__get_as_bry(Atr_key__srcset);
this.style = tag.Atrs__get_as_bry(Gfh_atr_.Bry__style);
this.data_pos_x = tag.Atrs__get_as_double_or(Atr_key__data_pos_x, 0);
this.data_pos_y = tag.Atrs__get_as_double_or(Atr_key__data_pos_y, 0);
}
public void Init_by_decode(double data_pos_x, double data_pos_y, byte[] srcset, byte[] style_if_not_dflt) {
this.data_pos_x = data_pos_x;
this.data_pos_y = data_pos_y;
this.srcset = srcset;
this.style = Bry_.Eq(style_if_not_dflt, Bry_.Empty) ? Atr_val__style_dflt : style_if_not_dflt;
this.exists = true;
}
public void Init_from_wtxt(Xoa_ttl banner_ttl, Xof_file_itm banner_file_itm, byte[] banner_anch_title, byte[] banner_hdr_text, boolean bottomtoc
, byte[] toc, double data_pos_x, double data_pos_y, byte[] originx, Pgbnr_icon[] icons) {
this.banner_ttl = banner_ttl; this.banner_file_itm = banner_file_itm;
this.banner_anch_title = banner_anch_title; this.banner_hdr_text = banner_hdr_text; this.bottomtoc = bottomtoc; this.toc = toc; this.icons = icons;
this.data_pos_x = data_pos_x; this.data_pos_y = data_pos_y; this.originx = originx;
this.banner_img_src = banner_file_itm.Html_view_url().To_http_file_bry();
this.file_ttl = Gfo_url_encoder_.Href_quotes.Encode(banner_file_itm.Lnki_ttl()); // NOTE: Encode(Lnki_ttl) not Orig_ttl; else "%27s" instead of "'s" PAGE:en.v:'s-Hertogenbosch; DATE:2016-07-12
}
public void Init_from_html(int max_width, byte[] banner_anch_href, byte[] banner_img_src, byte[] srcset, boolean isHeadingOverrideEnabled, byte[] toc) {
this.max_width = max_width;
this.banner_anch_href = banner_anch_href;
this.banner_img_src = banner_img_src;
this.srcset = srcset;
this.isHeadingOverrideEnabled = isHeadingOverrideEnabled;
this.toc = toc;
}
public void Init_hdump(boolean mode_is_hdump) {
Bry_bfr tmp_bfr = Bry_bfr_.New();
if (mode_is_hdump) {
this.img_id_atr = Bry_.Empty;
this.img_xottl_atr = Gfh_atr_.Add_to_bry(tmp_bfr, Xoh_img_xoimg_data.Bry__data_xowa_title, file_ttl);
this.img_xoimg_atr = Gfh_atr_.Add_to_bry(tmp_bfr, Xoh_img_xoimg_data.Bry__data_xowa_image, Xoh_img_xoimg_data.Bry__data_xowa_image__full);
this.banner_img_src = Bry_.Empty; // do not write img_src else hzip_diff will complain for every image; DATE:2016-10-18
}
else {
this.img_id_atr = tmp_bfr.Add(Bry__anch_atr_id).Add_int_variable(banner_file_itm.Html_uid()).Add_byte_quote().To_bry_and_clear();
this.img_xottl_atr = this.img_xoimg_atr = Bry_.Empty;
}
}
public boolean Mustache__write(String key, Mustache_bfr bfr) {
if (String_.Eq(key, "title")) bfr.Add_bry(banner_hdr_text);
else if (String_.Eq(key, "tooltip")) bfr.Add_bry(banner_anch_title);
else if (String_.Eq(key, "bannerfile")) bfr.Add_bry(banner_anch_href);
else if (String_.Eq(key, "banner")) bfr.Add_bry(banner_img_src);
else if (String_.Eq(key, "srcset")) bfr.Add_bry(srcset == null ? Bry_.Empty : Bry_.Empty);
else if (String_.Eq(key, "originx")) bfr.Add_bry(originx);
else if (String_.Eq(key, "data-pos-x")) bfr.Add_double(data_pos_x);
else if (String_.Eq(key, "data-pos-y")) bfr.Add_double(data_pos_y);
else if (String_.Eq(key, "maxWidth")) bfr.Add_int(max_width);
else if (String_.Eq(key, "toc")) bfr.Add_bry(toc);
else if (String_.Eq(key, "img_id_atr")) bfr.Add_bry(img_id_atr);
else if (String_.Eq(key, "img_xottl")) bfr.Add_bry(img_xottl_atr);
else if (String_.Eq(key, "img_xoimg")) bfr.Add_bry(img_xoimg_atr);
else if (String_.Eq(key, "file_ttl")) bfr.Add_bry(file_ttl);
else return false;
return true;
}
public Mustache_doc_itm[] Mustache__subs(String key) {
if (String_.Eq(key, "icons")) return icons;
else if (String_.Eq(key, "hasIcons")) return Mustache_doc_itm_.Ary__bool(icons.length > 0);
else if (String_.Eq(key, "bottomtoc")) return Mustache_doc_itm_.Ary__bool(bottomtoc);
else if (String_.Eq(key, "isHeadingOverrideEnabled")) return Mustache_doc_itm_.Ary__bool(isHeadingOverrideEnabled);
return Mustache_doc_itm_.Ary__empty;
}
private static final byte[] Bry__anch_atr_id = Bry_.new_a7(" id=\"xoimg_");
public static final byte[]
Atr_key__srcset = Bry_.new_a7("srcset")
, Atr_key__data_pos_x = Bry_.new_a7("data-pos-x")
, Atr_key__data_pos_y = Bry_.new_a7("data-pos-y")
, Atr_val__style_dflt = Bry_.new_a7("max-width:-1px")
;
}

View File

@@ -0,0 +1,103 @@
/*
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.brys.*; import gplx.langs.mustaches.*; import gplx.xowa.parsers.*;
import gplx.xowa.langs.*; import gplx.xowa.wikis.domains.*; import gplx.xowa.wikis.nss.*;
import gplx.xowa.htmls.core.htmls.*;
public class Pgbnr_xtn_mgr extends Xox_mgr_base implements Bfr_arg {
@Override public byte[] Xtn_key() {return Xtn_key_static;} public static final byte[] Xtn_key_static = Bry_.new_a7("pagebanner");
@Override public Xox_mgr Xtn_clone_new() {return new Pgbnr_xtn_mgr();}
public Pgbnr_cfg Cfg() {return cfg;} private Pgbnr_cfg cfg;
public Mustache_tkn_itm Template_root() {return template_root;} private Mustache_tkn_itm template_root;
@Override public void Xtn_init_by_app(Xoae_app app) {}
@Override public void Xtn_init_by_wiki(Xowe_wiki wiki) {
// load config; TODO_OLD: load by file
boolean enabled = false, enable_heading_override = true, enable_default_banner = false;
int[] ns_ary = Int_.Ary(Xow_ns_.Tid__main, Xow_ns_.Tid__user);
int[] standard_sizes = new int[] {640, 1280, 2560};
int dflt_img_wdata_prop = 948; byte[] dflt_img_title = Bry_.new_a7("Pagebanner_default.jpg"); // www.wikidata.org/wiki/Property:P948
switch (wiki.Domain_tid()) {
case Xow_domain_tid_.Tid__home:
enabled = true;
ns_ary = Int_.Ary(Xow_ns_.Tid__main);
break;
case Xow_domain_tid_.Tid__wikivoyage:
switch (wiki.Lang().Lang_id()) {
case Xol_lang_stub_.Id_en:
case Xol_lang_stub_.Id_fr:
case Xol_lang_stub_.Id_zh:
enabled = true;
break;
case Xol_lang_stub_.Id_ru:
ns_ary = Int_.Ary(0, 1, 10, 11, 12, 13, 14, 15, 2, 2300, 2301, 2302, 2303, 2600, 3, 4, 5, 6, 7, 8, 828, 829, 9);
enabled = true;
break;
case Xol_lang_stub_.Id_uk:
ns_ary = Int_.Ary(Xow_ns_.Tid__main, Xow_ns_.Tid__user, Xow_ns_.Tid__project);
enabled = true;
break;
}
break;
case Xow_domain_tid_.Tid__wikipedia:
switch (wiki.Lang().Lang_id()) {
case Xol_lang_stub_.Id_ca:
// enabled = enable_default_banner = true;
ns_ary = Int_.Ary(102, Xow_ns_.Tid__user);
break;
case Xol_lang_stub_.Id_en:
// enabled = enable_default_banner = true;
enable_heading_override = false;
break;
}
break;
}
cfg = new Pgbnr_cfg(enabled, enable_heading_override, enable_default_banner, ns_ary, dflt_img_wdata_prop, dflt_img_title, standard_sizes);
if (!enabled) return;
// load template file;
byte[] template_data = Io_mgr.Instance.LoadFilBryOr(wiki.Appe().Fsys_mgr().Bin_any_dir().GenSubDir_nest("xowa", "xtns", "WikidataPageBanner", "templates", "banner.mustache"), Template_dflt);
Mustache_tkn_parser parser = new Mustache_tkn_parser();
template_root = parser.Parse(template_data, 0, template_data.length);
}
public Bfr_arg Write_html(Xoae_page wpg, Xop_ctx pctx, Xoh_wtr_ctx hctx) {
this.wpg = wpg; this.pctx = pctx; this.hctx = hctx;
return this;
} private Xoae_page wpg; private Xop_ctx pctx; private Xoh_wtr_ctx hctx;
public void Bfr_arg__add(Bry_bfr bfr) {
Pgbnr_itm itm = wpg.Html_data().Xtn_pgbnr(); if (itm == null) return;
Pgbnr_func.Add_banner(bfr, wpg, pctx, hctx, itm);
}
public static final byte[] Bry__cls__wpb_banner_image = Bry_.new_a7("wpb-banner-image");
private static final byte[] Template_dflt = Bry_.New_u8_nl_apos
( "<div class='ext-wpb-pagebanner noprint pre-content'>"
, " <div class='wpb-topbanner'>"
, " {{#isHeadingOverrideEnabled}}<h1 class='wpb-name'>{{title}}</h1>{{/isHeadingOverrideEnabled}}"
, " <a href='{{bannerfile}}' class='image' title='{{tooltip}}' xowa_title='{{file_ttl}}'><img{{{img_id_atr}}}{{{img_xottl}}}{{{img_xoimg}}} src='{{banner}}' width='0' height='0' class='wpb-banner-image {{originx}}' alt='' srcset='{{srcset}}' data-pos-x='{{data-pos-x}}' data-pos-y='{{data-pos-y}}' style='max-width:{{maxWidth}}px'></a>"
, " {{#hasIcons}}"
, " <div class='wpb-iconbox'>"
, " {{#icons}}"
, " <a href='{{url}}'>{{{html}}}</a>"
, " {{/icons}}"
, " </div>"
, " {{/hasIcons}}"
, " </div>"
, " <div class='wpb-topbanner-toc{{#bottomtoc}} wpb-bottomtoc{{/bottomtoc}}'><div class='wpb-banner-toc'>{{{toc}}}</div></div>"
, "</div>"
);
}