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

TemplateStyles: Add templatestyles to html.head [#314]

This commit is contained in:
gnosygnu
2018-12-30 23:25:29 -05:00
parent e65d9268d6
commit cc502c872f
15 changed files with 211 additions and 57 deletions

View File

@@ -14,13 +14,16 @@ 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.template_styles; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.primitives.*;
import gplx.xowa.wikis.caches.*;
import gplx.xowa.htmls.core.htmls.*; import gplx.xowa.parsers.htmls.*;
import gplx.xowa.htmls.core.htmls.*; import gplx.xowa.parsers.htmls.*; import gplx.xowa.htmls.heads.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*;
import gplx.xowa.wikis.nss.*;
public class Template_styles_nde implements Xox_xnde, Mwh_atr_itm_owner2 {
private byte[] css_ttl_bry;
private byte[] css_src;
private boolean css_ignore;
private Int_obj_val css_page_id;
private Xoa_ttl css_ttl;
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, byte xatr_id) {
switch (xatr_id) {
@@ -45,31 +48,60 @@ public class Template_styles_nde implements Xox_xnde, Mwh_atr_itm_owner2 {
// get page
Xow_page_cache_itm page_itm = wiki.Cache_mgr().Page_cache().Get_or_load_as_itm_2(css_ttl);
css_src = page_itm == null ? null : page_itm.Wtxt__direct();
if (page_itm != null) {
css_src = page_itm.Wtxt__direct();
css_page_id = new Int_obj_val(page_itm.Page_id());
// update css_page_ids
Hash_adp css_page_ids = (Hash_adp)ctx.Page().Kv_data().Get_or_make(Template_styles_kv_itm.Instance);
if (css_page_ids.Has(css_page_id)) {
css_ignore = true;
}
else {
css_page_ids.Add_as_key_and_val(css_page_id);
}
}
if (css_src == null) {
Gfo_usr_dlg_.Instance.Warn_many("", "", "Template_styles_nde.page_not_found: wiki=~{0} page=~{1} css_ttl=~{2}", wiki.Domain_bry(), ctx.Page().Url_bry_safe(), css_ttl_bry);
}
}
}
public void Xtn_write(Bry_bfr bfr, Xoae_app app, Xop_ctx ctx, Xoh_html_wtr html_wtr, Xoh_wtr_ctx hctx, Xoae_page wpg, Xop_xnde_tkn xnde, byte[] src) {
if (css_ttl == null) {
bfr.Add_str_a7(formatTagError("Invalid title for TemplateStyles src attribute."));
return;
}
else if (css_src == null) {
if (css_src == null) {
bfr.Add_str_a7(formatTagError("Page " + String_.new_u8(css_ttl_bry) + " has no content."));
return;
}
else {
bfr.Add_str_a7("<style>");
bfr.Add(css_src);
bfr.Add_str_a7("</style>");
if (!css_ignore) {
Bry_bfr tmp_bfr = ctx.Wiki().Utl__bfr_mkr().Get_b512();
try {
html_head.Bld_many(tmp_bfr, css_page_id.Val(), css_src);
Xoh_head_itm__css_dynamic css_dynamic = ctx.Page().Html_data().Head_mgr().Itm__css_dynamic();
css_dynamic.Enabled_y_();
css_dynamic.Add(tmp_bfr.To_bry_and_clear());
} finally {tmp_bfr.Mkr_rls();}
}
}
private static String formatTagError(String msg) {
// $parser->addTrackingCategory( 'templatestyles-page-error-category' );
return "<strong class=\"error\">"
// + call_user_func_array( 'wfMessage', $msg )->inContentLanguage()->parse()
+ msg
+ "</strong>";
// + call_user_func_array( 'wfMessage', $msg )->inContentLanguage()->parse()
return html_error.Bld_many_to_str_auto_bfr(msg);
}
public static final byte Xatr__src = 0;
private static final Hash_adp_bry xatrs_hash = Hash_adp_bry.ci_a7().Add_str_byte("src", Xatr__src);
private static final Bry_fmt
html_head = Bry_fmt.Auto("\n/*TemplateStyles:r~{id}*/\n~{css}")
, html_error = Bry_fmt.Auto("<strong class=\"error\">~{msg}</strong>")
;
}
class Template_styles_kv_itm implements gplx.xowa.apps.kvs.Xoa_kv_itm {
public String Kv__key() {return "TemplateStyles";}
public Object Kv__val_make() {return Hash_adp_.New();}
public static final Template_styles_kv_itm Instance = new Template_styles_kv_itm(); Template_styles_kv_itm() {}
}

View File

@@ -14,52 +14,88 @@ 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.template_styles; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
import org.junit.*; import gplx.core.tests.*;
public class Template_styles_nde_tst {
private final Template_styles_nde_fxt fxt = new Template_styles_nde_fxt();
@Before public void init() {
fxt.Reset();
}
private static final String Css_red = ".red{color:red;}";
private static final String Style_red = "<style>" + Css_red + "</style>";
@Test public void Implicit_template() { // PURPOSE: default to template
fxt.Init__page("Template:Test.css", Css_red);
String css = fxt.Make__css_color("red");
fxt.Init__page("Template:Test.css", css);
fxt.Test__parse
( "<templatestyles src='Test.css'/>"
, Style_red
, ""
, fxt.Make__style(0, css)
);
}
@Test public void Force_main() { // PURPOSE: ":" forces main
fxt.Init__page("Test.css", Css_red);
String css = fxt.Make__css_color("red");
fxt.Init__page("Test.css", css);
fxt.Test__parse
( "<templatestyles src=':Test.css'/>"
, Style_red
, ""
, fxt.Make__style(0, css)
);
}
@Test public void Explicit() { // PURPOSE: explicit ns
fxt.Init__page("Module:Test.css", Css_red);
String css = fxt.Make__css_color("red");
fxt.Init__page("Module:Test.css", css);
fxt.Test__parse
( "<templatestyles src='Module:Test.css'/>"
, Style_red
, ""
, fxt.Make__style(0, css)
);
}
@Test public void Multiple() { // PURPOSE: multiple calls to diff page should output diff styles; DATE:2018-12-30
String css_red = fxt.Make__css_color("red");
String css_blue = fxt.Make__css_color("blue");
fxt.Init__page("Module:Test1.css", css_red);
fxt.Init__page("Module:Test2.css", css_blue);
fxt.Test__parse
( String_.Concat_lines_nl
( "<templatestyles src='Module:Test1.css'/>"
, "<templatestyles src='Module:Test2.css'/>"
)
, ""
, fxt.Make__style(0, css_red)
+ fxt.Make__style(1, css_blue)
);
}
@Test public void Dedupe() { // PURPOSE: multiple calls to same page should output link; DATE:2018-12-30
String css = fxt.Make__css_color("red");
fxt.Init__page("Module:Test.css", css);
fxt.Test__parse
( String_.Concat_lines_nl
( "<templatestyles src='Module:Test.css'/>"
, "<templatestyles src='Module:Test.css'/>"
)
, ""
, String_.Concat_lines_nl
( fxt.Make__style(0, css))
);
}
@Test public void Tag() { // PURPOSE: {{#tag}}
fxt.Init__page("Module:A/Test.css", Css_red);
String css = fxt.Make__css_color("red");
fxt.Init__page("Module:A/Test.css", css);
fxt.Test__parse
( "{{#tag:templatestyles||src='Module:A/Test.css'}}"
, Style_red
, ""
, fxt.Make__style(0, css)
);
}
@Test public void Error__invalid_title() {
fxt.Test__parse
( "<templatestyles src='A|b.css'/>"
, "<strong class=\"error\">Invalid title for TemplateStyles src attribute.</strong>"
, ""
);
}
@Test public void Error__missing_page() {
fxt.Test__parse
( "<templatestyles src='Missing.css'/>"
, "<strong class=\"error\">Page Missing.css has no content.</strong>"
, ""
);
}
}
@@ -72,7 +108,14 @@ class Template_styles_nde_fxt {
public void Init__page(String page, String text) {
parser_fxt.Init_page_create(page, text);
}
public void Test__parse(String src, String expd) {
parser_fxt.Test__parse__tmpl_to_html(src, expd);
public String Make__css_color(String color) {
return ".style0{color:" + color + ";}";
}
public String Make__style(int id, String css) {
return "\n/*TemplateStyles:r" + id + "*/\n" + css;
}
public void Test__parse(String src, String expd_html, String expd_head) {
parser_fxt.Test__parse__tmpl_to_html(src, expd_html);
Gftest.Eq__ary__lines(expd_head, parser_fxt.Page().Html_data().Head_mgr().Itm__css_dynamic().Get_and_clear());
}
}