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

Table_of_contents: Ignore NOTOC commands at template level

This commit is contained in:
gnosygnu
2017-06-17 10:34:29 -04:00
parent a439f53c1a
commit e2e68342e0
13 changed files with 121 additions and 24 deletions

View File

@@ -51,6 +51,7 @@ public class Xop_ctx {
public Xop_curly_wkr Curly() {return curly;} private final Xop_curly_wkr curly = new Xop_curly_wkr();
public Xop_xnde_tag_regy Xnde_tag_regy() {return xnde_tag_regy;} private final Xop_xnde_tag_regy xnde_tag_regy; // PERF:demeter
public Xop_tmp_mgr Tmp_mgr() {return tmp_mgr;} private final Xop_tmp_mgr tmp_mgr = new Xop_tmp_mgr();
public Xop_ctx_page_data Page_data() {return page_data;} private final Xop_ctx_page_data page_data = new Xop_ctx_page_data();
public byte Xnde_names_tid() {return xnde_names_tid;} public Xop_ctx Xnde_names_tid_(byte v) {xnde_names_tid = v; return this;} private byte xnde_names_tid = Xop_parser_tid_.Tid__null;
public byte Parse_tid() {return parse_tid;} public Xop_ctx Parse_tid_(byte v) {parse_tid = v; xnde_names_tid = v; return this;} private byte parse_tid = Xop_parser_tid_.Tid__null;
@@ -85,6 +86,7 @@ public class Xop_ctx {
if (lst_section_mgr != null) lst_section_mgr.Clear();
if (lst_page_regy != null) lst_page_regy.Clear();
tmpl_args_parsing = false;
page_data.Clear();
return this;
}
public String Page_url_str() {

View File

@@ -0,0 +1,35 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.parsers; import gplx.*; import gplx.xowa.*;
public class Xop_ctx_page_data {
public boolean Hdr_toc() {return toc;} public void Hdr_toc_y_() {this.toc = true;} private boolean toc;
public boolean Hdr_forcetoc() {return forcetoc;} public void Hdr_forcetoc_y_() {this.forcetoc = true;} private boolean forcetoc;
public boolean Hdr_notoc() {return notoc;} public void Hdr_notoc_y_() {this.notoc = true;} private boolean notoc;
public boolean Lang_convert_content() {return lang_convert_content;} public void Lang_convert_content_(boolean v) {this.lang_convert_content = v;} private boolean lang_convert_content = true;
public boolean Lang_convert_title() {return lang_convert_title;} public void Lang_convert_title_(boolean v) {this.lang_convert_title = v;} private boolean lang_convert_title = true;
public void Clear() {
toc = forcetoc = notoc = false;
lang_convert_content = lang_convert_title = true;
}
public void Copy_to(Xoae_page page) {
gplx.xowa.wikis.pages.wtxts.Xopg_toc_mgr hdr_mgr = page.Wtxt().Toc();
hdr_mgr.Flag__toc_(toc);
hdr_mgr.Flag__forcetoc_(forcetoc);
hdr_mgr.Flag__notoc_(notoc);
page.Html_data().Lang_convert_content_(lang_convert_content);
page.Html_data().Lang_convert_title_(lang_convert_title);
}
}

View File

@@ -87,8 +87,11 @@ public class Xow_parser_mgr {
scrib.When_page_changed(page); // notify scribunto about page changed
ctx.Page_(page);
ctx.Page_data().Clear(); // DATE:2017-06-13
Xop_root_tkn root = ctx.Tkn_mkr().Root(page.Db().Text().Text_bry());
if (clear) {page.Clear_all();}
if (clear) {
page.Clear_all();
}
Xoa_ttl ttl = page.Ttl();
if ( Xow_page_tid.Identify(wiki.Domain_tid(), ttl.Ns().Id(), ttl.Page_db()) == Xow_page_tid.Tid_wikitext) { // only parse page if wikitext; skip .js, .css, Module; DATE:2013-11-10
byte[] data_raw = page.Db().Text().Text_bry();
@@ -96,5 +99,7 @@ public class Xow_parser_mgr {
}
page.Root_(root);
root.Data_htm_(root.Root_src());
ctx.Page_data().Copy_to(page); // copy __TOC__ from ctx to page; needed to prevent template from affecting page output; DATE:2017-06-11
}
}

View File

@@ -97,19 +97,17 @@ public class Xop_under_lxr implements Xop_lxr {
return rv;
}
public static void 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 kwd_id) {
Xoae_page page = ctx.Page();
Xopg_toc_mgr hdr_mgr = page.Wtxt().Toc();
switch (kwd_id) {
case Xol_kwd_grp_.Id_toc:
hdr_mgr.Flag__toc_y_();
ctx.Page_data().Hdr_toc_y_();
ctx.Para().Process_block_lnki_div(); // NOTE: __TOC__ will manually place <div toc> here; simulate div in order to close any pres; EX:\n\s__TOC__; PAGE:de.w: DATE:2014-07-05
ctx.Subs_add(root, tkn_mkr.Under(bgn_pos, cur_pos, kwd_id)); // NOTE: only save under_tkn for TOC (b/c its position is needed for insertion); DATE:2013-07-01
break;
case Xol_kwd_grp_.Id_forcetoc: hdr_mgr.Flag__forcetoc_y_(); break;
case Xol_kwd_grp_.Id_notoc: hdr_mgr.Flag__notoc_y_(); break;
case Xol_kwd_grp_.Id_forcetoc: ctx.Page_data().Hdr_forcetoc_y_(); break;
case Xol_kwd_grp_.Id_notoc: ctx.Page_data().Hdr_notoc_y_(); break;
case Xol_kwd_grp_.Id_noeditsection: break; // ignore; not handling edit sections
case Xol_kwd_grp_.Id_nocontentconvert: page.Html_data().Lang_convert_content_(false); break;
case Xol_kwd_grp_.Id_notitleconvert: page.Html_data().Lang_convert_title_(false); break;
case Xol_kwd_grp_.Id_nocontentconvert: ctx.Page_data().Lang_convert_content_(false); break;
case Xol_kwd_grp_.Id_notitleconvert: ctx.Page_data().Lang_convert_title_(false); break;
default: break; // ignore anything else
}
}

View File

@@ -101,12 +101,12 @@ public class Xop_under_lxr_tst {
fxt.Test_parse_page_all_str("__DISAMBIG__", "");
}
@Test public void Nocontentconvert() { // simple test; test for flag only; DATE:2014-02-06
gplx.xowa.wikis.pages.htmls.Xopg_html_data html_data = fxt.Page().Html_data();
Tfds.Eq(html_data.Lang_convert_content(), true);
Tfds.Eq(html_data.Lang_convert_title(), true);
gplx.xowa.parsers.Xop_ctx_page_data page_data = fxt.Ctx().Page_data();
Tfds.Eq(page_data.Lang_convert_content(), true);
Tfds.Eq(page_data.Lang_convert_title(), true);
fxt.Test_parse_page_all_str("__NOCONTENTCONVERT__ __NOTITLECONVERT__", " ");
Tfds.Eq(html_data.Lang_convert_content(), false);
Tfds.Eq(html_data.Lang_convert_title(), false);
Tfds.Eq(page_data.Lang_convert_content(), false);
Tfds.Eq(page_data.Lang_convert_title(), false);
}
@Test public void Eos() { // PURPOSE: check that __ at eos doesn't fail; es.s:Luisa de Bustamante: 3; DATE:2014-02-15
fxt.Test_parse_page_all_str("__", "__");
@@ -187,4 +187,40 @@ public class Xop_under_lxr_tst {
fxt.Test_parse_page_all_str("a__NOTOC__b" , "ab"); // cs.pass
fxt.Test_parse_page_all_str("a__notoc__b" , "a__notoc__b"); // cs.fail
}
@Test public void Notoc_in_tmpl() { // PURPOSE: test simultaneous cs and ci; DATE:2017-06-11
fxt.Init_page_create("Template:Notoc", "page_text\n__NOTOC__");
fxt.Wiki().Html_mgr().Html_wtr().Cfg().Toc__show_(true);
fxt.Test__parse_to_html_w_skin(String_.Concat_lines_nl
( "==A1=="
, "==A2=="
, "==A3=="
, "==A4=="
, "{{Notoc}}"
), String_.Concat_lines_nl
( "<div id=\"toc\" class=\"toc\">"
, " <div id=\"toctitle\">"
, " <h2>Contents</h2>"
, " </div>"
, " <ul>"
, " <li class=\"toclevel-1 tocsection-1\"><a href=\"#A1\"><span class=\"tocnumber\">1</span> <span class=\"toctext\">A1</span></a>"
, " </li>"
, " <li class=\"toclevel-1 tocsection-2\"><a href=\"#A2\"><span class=\"tocnumber\">2</span> <span class=\"toctext\">A2</span></a>"
, " </li>"
, " <li class=\"toclevel-1 tocsection-3\"><a href=\"#A3\"><span class=\"tocnumber\">3</span> <span class=\"toctext\">A3</span></a>"
, " </li>"
, " <li class=\"toclevel-1 tocsection-4\"><a href=\"#A4\"><span class=\"tocnumber\">4</span> <span class=\"toctext\">A4</span></a>"
, " </li>"
, " </ul>"
, "</div>"
, "<h2><span class=\"mw-headline\" id=\"A1\">A1</span></h2>"
, ""
, "<h2><span class=\"mw-headline\" id=\"A2\">A2</span></h2>"
, ""
, "<h2><span class=\"mw-headline\" id=\"A3\">A3</span></h2>"
, ""
, "<h2><span class=\"mw-headline\" id=\"A4\">A4</span></h2>"
, "page_text"
));
}
}

View File

@@ -285,7 +285,7 @@ public class Xot_invk_tkn extends Xop_tkn_itm_base implements Xot_invk {
bfr.Add(rslt);
}
else {
rv = defn_tmpl.Tmpl_evaluate(ctx, invk_tmpl, rslt_bfr);
rv = defn_tmpl.Tmpl_evaluate(Xop_ctx.New__sub(wiki, ctx, ctx.Page()), invk_tmpl, rslt_bfr); // DATE:2017-06-13
prepend_mgr.End(ctx, bfr, rslt_bfr.Bfr(), rslt_bfr.Len(), Bool_.Y);
if (name_had_subst) { // current invk had "subst:"; parse incoming invk again to remove effects of subst; PAGE:pt.w:Argentina DATE:2014-09-24
byte[] tmp_src = rslt_bfr.To_bry_and_clear();

View File

@@ -55,7 +55,7 @@ public class Xot_invk_tkn_ {
if (tmpl_page_bry != null) {
byte old_parse_tid = ctx.Parse_tid(); // NOTE: reusing ctxs is a bad idea; will change Parse_tid and cause strange errors; however, keeping for PERF reasons
Xow_ns ns_tmpl = wiki.Ns_mgr().Ns_template();
rv = wiki.Parser_mgr().Main().Parse_text_to_defn_obj(ctx, ctx.Tkn_mkr(), ns_tmpl, name_ary, tmpl_page_bry);
rv = wiki.Parser_mgr().Main().Parse_text_to_defn_obj(Xop_ctx.New__sub(wiki, ctx, ctx.Page()), ctx.Tkn_mkr(), ns_tmpl, name_ary, tmpl_page_bry); // DATE:2017-06-13
Xoa_ttl tmpl_page_ttl = tmpl_page_itm.Ttl();
byte[] frame_ttl = Bry_.Add(tmpl_page_ttl.Ns().Name_db(), Byte_ascii.Colon_bry, tmpl_page_ttl.Page_txt()); // NOTE: (1) must have ns (Full); (2) must be txt (space, not underscore); EX:Template:Location map+; DATE:2014-08-22; (3) must be local language; Russian "Шаблон" not English "Template"; PAGE:ru.w:Королевство_Нидерландов DATE:2016-11-23
rv.Frame_ttl_(frame_ttl); // set defn's frame_ttl; needed for redirect_trg; PAGE:en.w:Statutory_city; DATE:2014-08-22