mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v3.1.5.1
This commit is contained in:
@@ -16,16 +16,25 @@ 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 static final int[] wpb_standard_sizes = new int[] {640, 1280, 2560};
|
||||
public byte[] Default_file() {return null;}
|
||||
public boolean Enabled_in_ns(int ns_id) {return false;}
|
||||
public boolean Get__wpb_enable_default_banner() {return false;}
|
||||
public boolean Get__wpb_enable_heading_override() {return false;}
|
||||
public byte[] Get__wpb_image() {return null;}
|
||||
public int[] Get__wpb_standard_sizes() {return wpb_standard_sizes;}
|
||||
private final Hash_adp ns_hash = Hash_adp_.new_(); private final Int_obj_ref tmp_ns_key = Int_obj_ref.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) {
|
||||
return this.Enabled_in_ns(ttl.Ns().Id()) // chk if ns allows banner
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,16 @@ 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.*; 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.*;
|
||||
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();
|
||||
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 = ctx.Page().Ttl();
|
||||
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;;
|
||||
@@ -34,10 +34,13 @@ public class Pgbnr_func extends Pf_func_base {
|
||||
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);
|
||||
byte[] key = Pf_func_.Eval_tkn(tmp_bfr, ctx, src, caller, arg.Key_tkn());
|
||||
byte[] val = Pf_func_.Eval_tkn(tmp_bfr, ctx, src, caller, arg.Val_tkn());
|
||||
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;
|
||||
@@ -47,9 +50,9 @@ public class Pgbnr_func extends Pf_func_base {
|
||||
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 // handle "icon-*";
|
||||
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(Bry__icon) > 5 // if ( !isset( $iconname) )
|
||||
&& Bry_.Len(key) > 5 // if ( !isset( $iconname) )
|
||||
&& Bry_.Len_gt_0(val) // if ( !isset( $$value ) )
|
||||
) { // REF.MW:addIcons
|
||||
tid = Arg__icon;
|
||||
@@ -57,16 +60,13 @@ public class Pgbnr_func extends Pf_func_base {
|
||||
byte[] icon_key = Bry_.Mid(key, 5);
|
||||
byte[] icon_name = Xop_sanitizer.Escape_cls(icon_key);
|
||||
byte[] icon_title = icon_name;
|
||||
Xoa_ttl icon_url_ttl = wiki.Ttl_parse(val);
|
||||
// TODO: get icon_url
|
||||
byte[] icon_url_bry = Bry_.Empty;
|
||||
if (icon_url_ttl == null)
|
||||
icon_url_bry = Bry__url_dflt; // $iconUrl = Title::newFromText( $value ); if ( $iconUrl )
|
||||
else {
|
||||
icon_url_bry = Bry_.Empty; // $iconUrl->getLocalUrl();
|
||||
icon_title = ttl.Page_txt();
|
||||
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(icon_name, icon_title, icon_url_bry));
|
||||
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;
|
||||
@@ -85,86 +85,82 @@ public class Pgbnr_func extends Pf_func_base {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tid == -1) Gfo_usr_dlg_.Instance.Warn_many("", "", "unknown arg type; page=~{0} key=~{1} val=~{2}", "page", ctx.Page().Url_bry_safe(), key, val);
|
||||
if (tid == -1) Gfo_usr_dlg_.Instance.Warn_many("", "", "unknown arg type; page=~{0} key=~{1} val=~{2}", page.Url_bry_safe(), key, val);
|
||||
}
|
||||
byte[] name = Eval_argx(ctx, src, caller, self);
|
||||
// Xoa_ttl file_ttl = wiki.Ttl_parse(name); SKIP?: MW creates title to auto-register page and image in imagelinks
|
||||
|
||||
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
|
||||
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();
|
||||
ctx.Page().Html_data().Xtn__pgbnr_(itm);
|
||||
itm.Init_from_wtxt(name, 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));
|
||||
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
|
||||
}
|
||||
public static void Add_banner(Bry_bfr bfr, Xop_ctx ctx) {
|
||||
Xowe_wiki wiki = ctx.Wiki();
|
||||
Pgbnr_cfg cfg = wiki.Xtn_mgr().Xtn_pgbnr().Cfg();
|
||||
Xoa_ttl ttl = ctx.Page().Ttl();
|
||||
Pgbnr_itm itm = ctx.Page().Html_data().Xtn__pgbnr();
|
||||
byte[] banner_name = null, banner_html = null;
|
||||
if (itm != null) { // {{PAGEBANNER}} exists in wikitext
|
||||
banner_name = itm.name;
|
||||
banner_html = Get_banner_html(wiki, cfg, banner_name, itm);
|
||||
if (banner_html == null) { // no banner; try again using title from wikidata;
|
||||
banner_name = Get_wikidata_banner(ttl);
|
||||
banner_html = Get_banner_html(wiki, cfg, banner_name, itm);
|
||||
}
|
||||
if (banner_html != null) { // only add banner and styling if valid banner generated
|
||||
if (itm.toc != null) {
|
||||
// $out->addModuleStyles( 'ext.WikidataPageBanner.toc.styles' );
|
||||
}
|
||||
bfr.Add(banner_html);
|
||||
public static void Add_banner(Bry_bfr bfr, Xop_ctx ctx, Xoh_wtr_ctx hctx, byte[] src) {
|
||||
Xowe_wiki wiki = ctx.Wiki(); Xoae_app app = wiki.Appe();
|
||||
Pgbnr_cfg cfg = wiki.Xtn_mgr().Xtn_pgbnr().Cfg(); if (!cfg.enabled) return;
|
||||
Xoae_page page = ctx.Page(); Xoa_ttl ttl = page.Ttl();
|
||||
Pgbnr_itm itm = page.Html_data().Xtn_pgbnr();
|
||||
Xoa_ttl banner_ttl = null; byte[] banner_html = null;
|
||||
if (itm != null) { // {{PAGEBANNER}} exists in wikitext
|
||||
banner_ttl = itm.banner_ttl;
|
||||
banner_html = Get_banner_html(wiki, ctx, src, 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, src, cfg, banner_ttl, itm);
|
||||
}
|
||||
}
|
||||
else if ( ttl.Ns().Id_is_main() // if the page uses no 'PAGEBANNER' invocation and if article page, insert default banner
|
||||
&& cfg.Get__wpb_enable_default_banner()
|
||||
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)
|
||||
) {
|
||||
if (cfg.Chk_pgbnr_allowed(ttl, wiki)) {
|
||||
banner_name = Get_wikidata_banner(ttl);
|
||||
if (banner_name == null) {
|
||||
banner_name = cfg.Get__wpb_image();
|
||||
}
|
||||
itm = new Pgbnr_itm();
|
||||
itm.name = banner_name;
|
||||
banner_html = Get_banner_html(wiki, cfg, banner_name, itm);
|
||||
if (banner_html != null) { // NOTE: same as above
|
||||
bfr.Add(banner_html);
|
||||
}
|
||||
}
|
||||
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);
|
||||
banner_html = Get_banner_html(wiki, ctx, src, cfg, banner_ttl, itm);
|
||||
}
|
||||
if (banner_html != null)
|
||||
bfr.Add(banner_html);
|
||||
}
|
||||
public static byte[] Get_banner_html(Xowe_wiki wiki, Pgbnr_cfg cfg, byte[] banner_name, Pgbnr_itm itm) {
|
||||
byte[][] urls = Get_standard_size_urls(wiki, cfg, banner_name);
|
||||
if (urls == null) return null;
|
||||
public static byte[] Get_banner_html(Xowe_wiki wiki, Xop_ctx ctx, byte[] src, 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.Get__wpb_standard_sizes();
|
||||
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); // $srcset[] = "$url {$size}w";
|
||||
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 = urls.length == 0 ? Bry_.Empty : urls[urls_len - 1]; // gets largest url
|
||||
// Xoa_ttl banner_file = wiki.Ttl_parse(tmp_bfr.Add(wiki.Ns_mgr().Ns_file().Name_db_w_colon()).Add(banner_name).To_bry_and_clear());
|
||||
int maxWidth = 1; // $file = wfFindFile( banner_file ); $options['maxWidth'] = $file->getWidth();
|
||||
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();
|
||||
itm.Init_from_html(maxWidth, banner_file, banner_url, srcset, cfg.Get__wpb_enable_heading_override());
|
||||
|
||||
wiki.Html_mgr().Toc_mgr().Html(ctx.Page(), gplx.xowa.htmls.core.htmls.Xoh_wtr_ctx.Basic, src, tmp_bfr, false);
|
||||
byte[] 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);
|
||||
wiki.Xtn_mgr().Xtn_pgbnr().Template_root().Render(tmp_bfr, mctx);
|
||||
return tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
private static byte[][] Get_standard_size_urls(Xow_wiki wiki, Pgbnr_cfg cfg, byte[] file_name) {
|
||||
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.Get__wpb_standard_sizes();
|
||||
int[] sizes = cfg.standard_sizes;
|
||||
int sizes_len = sizes.length;
|
||||
for (int i = 0; i < sizes_len; ++i) {
|
||||
byte[] url = Get_image_url(wiki, file_name, sizes[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, byte[] file_name, int width) {
|
||||
// Xoa_ttl file_ttl = wiki.Ttl_parse(file_name);
|
||||
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) {
|
||||
@@ -175,10 +171,19 @@ public class Pgbnr_func extends Pf_func_base {
|
||||
else
|
||||
return new byte[0]; // $file->getFullUrl();
|
||||
}
|
||||
private static byte[] Get_wikidata_banner(Xoa_ttl ttl) {
|
||||
return null;
|
||||
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 final byte[] Bry__yes = Bry_.new_a7("yes"), Bry__icon = Bry_.new_a7("icon-"), Bry__url_dflt = Bry_.new_a7("#"), Bry__origin_x__left = Bry_.new_a7("wpb-left"), Bry__origin_x__right = Bry_.new_a7("wpb-right");
|
||||
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);
|
||||
|
||||
@@ -18,24 +18,32 @@ 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.*;
|
||||
public class Pgbnr_func_tst {
|
||||
// private final Pgbnr_func_fxt fxt = new Pgbnr_func_fxt();
|
||||
private final Pgbnr_func_fxt fxt = new Pgbnr_func_fxt();
|
||||
@Test public void Basic() {
|
||||
// fxt.Test__parse__eq("{{PAGEBANNER:A.png}}", 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 class='image' title='Test page' href=''><img src='' srcset=' 640w' class='wpb-banner-image ' data-pos-x='0' data-pos-y='0' style='max-width:1px'></a>"
|
||||
// , " </div>"
|
||||
// , " <div class='wpb-topbanner-toc '><div class='wpb-banner-toc'></div></div>"
|
||||
// , "</div>"
|
||||
// ));
|
||||
fxt.Test__parse__eq("{{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 class='image' title='Test page' href=''><img id='xoimg_0' src='file:///mem/wiki/repo/trg/orig/7/0/A.png' srcset='' class='wpb-banner-image ' 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>"
|
||||
, "</div>"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Pgbnr_func_fxt {
|
||||
private final Xop_fxt fxt = new Xop_fxt();
|
||||
private final Xop_fxt fxt;
|
||||
public Pgbnr_func_fxt() {
|
||||
Xoae_app app = Xoa_app_fxt.app_();
|
||||
Xowe_wiki wiki = Xoa_app_fxt.wiki_(app, "en.wikivoyage.org");
|
||||
fxt = new Xop_fxt(app, wiki);
|
||||
}
|
||||
public void Test__parse__eq(String raw, String expd) {
|
||||
fxt.Exec_parse_page_all_as_str("{{PAGEBANNER:A.png}}");
|
||||
Bfr_arg arg = fxt.Wiki().Xtn_mgr().Xtn_pgbnr().Write_html(fxt.Ctx(), fxt.Page());
|
||||
fxt.Exec_parse_page_all_as_str(raw);
|
||||
Bfr_arg arg = fxt.Wiki().Xtn_mgr().Xtn_pgbnr().Write_html(fxt.Ctx(), fxt.Page(), null, null);
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
arg.Bfr_arg__add(bfr);
|
||||
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
|
||||
|
||||
@@ -16,19 +16,26 @@ 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 {
|
||||
public byte[] name;
|
||||
public byte[] title;
|
||||
public byte[] url;
|
||||
public Pgbnr_icon(byte[] name, byte[] title, byte[] url) {this.name = name; this.title = title; this.url = url;}
|
||||
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_bfr_many(tmp_bfr, name, title);
|
||||
this.html = tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
public byte[] Get_prop(String key) {
|
||||
if (String_.Eq(key, "name")) return name;
|
||||
else if (String_.Eq(key, "title")) return title;
|
||||
else if (String_.Eq(key, "url")) return url;
|
||||
else if (String_.Eq(key, "html")) return Mustache_doc_itm_.Null_val;
|
||||
else if (String_.Eq(key, "url")) return href;
|
||||
else if (String_.Eq(key, "html")) return html;
|
||||
return Mustache_doc_itm_.Null_val;
|
||||
}
|
||||
public Mustache_doc_itm[] Get_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"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,32 +16,41 @@ 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.langs.mustaches.*; import gplx.xowa.parsers.lnkis.*;
|
||||
import gplx.xowa.files.*;
|
||||
public class Pgbnr_itm implements Mustache_doc_itm {
|
||||
public byte[] name, toc;
|
||||
private byte[] tooltip, title, originx, bannerfile, banner, srcset;
|
||||
public Xoa_ttl banner_ttl;
|
||||
public byte[] banner_img_src;
|
||||
private int html_uid;
|
||||
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 maxWidth;
|
||||
private boolean bottomtoc, isHeadingOverrideEnabled;
|
||||
private Pgbnr_icon[] icons;
|
||||
public void Init_from_wtxt(byte[] name, byte[] tooltip, byte[] title, boolean bottomtoc, byte[] toc, double data_pos_x, double data_pos_y, byte[] originx, Pgbnr_icon[] icons) {
|
||||
this.name = name;
|
||||
this.tooltip = tooltip; this.title = title; this.bottomtoc = bottomtoc; this.toc = toc; this.icons = icons;
|
||||
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.html_uid = banner_file_itm.Html_uid();
|
||||
this.banner_img_src = banner_file_itm.Html_view_url().To_http_file_bry();
|
||||
}
|
||||
public void Init_from_html(int maxWidth, byte[] bannerfile, byte[] banner, byte[] srcset, boolean isHeadingOverrideEnabled) {
|
||||
public void Init_from_html(int maxWidth, byte[] banner_anch_href, byte[] banner_img_src, byte[] srcset, boolean isHeadingOverrideEnabled, byte[] toc) {
|
||||
this.maxWidth = maxWidth;
|
||||
this.bannerfile = bannerfile;
|
||||
this.banner = banner;
|
||||
this.banner_anch_href = banner_anch_href;
|
||||
this.banner_img_src = banner_img_src;
|
||||
this.srcset = srcset;
|
||||
this.isHeadingOverrideEnabled = isHeadingOverrideEnabled;
|
||||
this.toc = toc;
|
||||
}
|
||||
public byte[] Get_prop(String key) {
|
||||
if (String_.Eq(key, "title")) return title;
|
||||
else if (String_.Eq(key, "tooltip")) return tooltip;
|
||||
else if (String_.Eq(key, "bannerfile")) return bannerfile;
|
||||
else if (String_.Eq(key, "banner")) return banner;
|
||||
else if (String_.Eq(key, "srcset")) return srcset;
|
||||
if (String_.Eq(key, "title")) return banner_hdr_text;
|
||||
else if (String_.Eq(key, "tooltip")) return banner_anch_title;
|
||||
else if (String_.Eq(key, "bannerfile")) return banner_anch_href;
|
||||
else if (String_.Eq(key, "banner")) return banner_img_src;
|
||||
else if (String_.Eq(key, "html_uid")) return Bry_.new_u8(gplx.xowa.htmls.Xoh_img_mgr.Str__html_uid + Int_.To_str(html_uid));
|
||||
else if (String_.Eq(key, "srcset")) return srcset == null ? Bry_.Empty : Bry_.Empty;
|
||||
else if (String_.Eq(key, "originx")) return originx;
|
||||
else if (String_.Eq(key, "data-pos-x")) return Bry_.new_a7(Double_.To_str(data_pos_x));
|
||||
else if (String_.Eq(key, "data-pos-y")) return Bry_.new_a7(Double_.To_str(data_pos_y));
|
||||
|
||||
@@ -17,33 +17,77 @@ 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 Clone_new() {return new Pgbnr_xtn_mgr();}
|
||||
public Pgbnr_cfg Cfg() {return cfg;} private final Pgbnr_cfg cfg = new Pgbnr_cfg();
|
||||
public Mustache_tkn_itm Template_root() {
|
||||
if (template_root == null) {
|
||||
Mustache_tkn_parser parser = new Mustache_tkn_parser();
|
||||
template_root = parser.Parse(Template_dflt, 0, Template_dflt.length);
|
||||
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: 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_.Int__home:
|
||||
enabled = true;
|
||||
ns_ary = Int_.Ary(Xow_ns_.Tid__help);
|
||||
break;
|
||||
case Xow_domain_tid_.Int__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_.Int__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;
|
||||
}
|
||||
return template_root;
|
||||
} private Mustache_tkn_itm template_root;
|
||||
@Override public void Xtn_init_by_app(Xoae_app app) {
|
||||
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(Xop_ctx pctx, Xoae_page page) {
|
||||
this.pctx = pctx; this.page = page;
|
||||
public Bfr_arg Write_html(Xop_ctx pctx, Xoae_page page, Xoh_wtr_ctx hctx, byte[] src) {
|
||||
this.pctx = pctx; this.page = page; this.hctx = hctx; this.src = src;
|
||||
return this;
|
||||
} private Xop_ctx pctx; private Xoae_page page;
|
||||
} private Xop_ctx pctx; private Xoae_page page; private Xoh_wtr_ctx hctx; private byte[] src;
|
||||
public void Bfr_arg__add(Bry_bfr bfr) {
|
||||
Pgbnr_itm itm = page.Html_data().Xtn__pgbnr();
|
||||
Pgbnr_itm itm = page.Html_data().Xtn_pgbnr();
|
||||
if (itm == null) return;
|
||||
Pgbnr_func.Add_banner(bfr, pctx);
|
||||
Pgbnr_func.Add_banner(bfr, pctx, hctx, src);
|
||||
}
|
||||
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 class='image' title='{{tooltip}}' href='{{bannerfile}}'><img src='{{banner}}' srcset='{{srcset}}' class='wpb-banner-image {{originx}}' data-pos-x='{{data-pos-x}}' data-pos-y='{{data-pos-y}}' style='max-width:{{maxWidth}}px'></a>"
|
||||
, " <a class='image' title='{{tooltip}}' href='{{bannerfile}}'><img id='{{html_uid}}' src='{{banner}}' srcset='{{srcset}}' class='wpb-banner-image {{originx}}' data-pos-x='{{data-pos-x}}' data-pos-y='{{data-pos-y}}' style='max-width:{{maxWidth}}px'></a>"
|
||||
, " {{#hasIcons}}"
|
||||
, " <div class='wpb-iconbox'>"
|
||||
, " {{#icons}}"
|
||||
|
||||
Reference in New Issue
Block a user