diff --git a/copyparty/__main__.py b/copyparty/__main__.py index 1a0009f2..9b75b1bb 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -1776,6 +1776,7 @@ def add_ui(ap, retry: int): ap2.add_argument("--js-browser", metavar="L", type=u, default="", help="URL to additional JS to include in the filebrowser html") ap2.add_argument("--js-other", metavar="L", type=u, default="", help="URL to additional JS to include in all other pages") ap2.add_argument("--html-head", metavar="TXT", type=u, default="", help="text to append to the of all HTML pages (except for basic-browser); can be @PATH to send the contents of a file at PATH, and/or begin with %% to render as jinja2 template (volflag=html_head)") + ap2.add_argument("--html-head-s", metavar="T", type=u, default="", help="text to append to the of all HTML pages (except for basic-browser); similar to (and can be combined with) --html-head but only accepts static text (volflag=html_head_s)") ap2.add_argument("--ih", action="store_true", help="if a folder contains index.html, show that instead of the directory listing by default (can be changed in the client settings UI, or add ?v to URL for override)") ap2.add_argument("--textfiles", metavar="CSV", type=u, default="txt,nfo,diz,cue,readme", help="file extensions to present as plaintext") ap2.add_argument("--txt-max", metavar="KiB", type=int, default=64, help="max size of embedded textfiles on ?doc= (anything bigger will be lazy-loaded by JS)") diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index 69f4caf2..daa0c21a 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -2493,6 +2493,31 @@ class AuthSrv(object): t = "WARNING: volume [/%s]: invalid value specified for ext-th: %s" self.log(t % (vol.vpath, etv), 3) + zs = str(vol.flags.get("html_head") or "") + if zs and zs[:1] in "%@": + vol.flags["html_head_d"] = zs + head_s = str(vol.flags.get("html_head_s") or "") + else: + zs2 = str(vol.flags.get("html_head_s") or "") + if zs2 and zs: + head_s = "%s\n%s\n" % (zs2.strip(), zs.strip()) + else: + head_s = zs2 or zs + + if head_s and not head_s.endswith("\n"): + head_s += "\n" + + if "norobots" in vol.flags: + head_s += META_NOBOTS + + if head_s: + vol.flags["html_head_s"] = head_s + else: + vol.flags.pop("html_head_s", None) + + if not vol.flags.get("html_head_d"): + vol.flags.pop("html_head_d", None) + vol.check_landmarks() # d2d drops all database features for a volume diff --git a/copyparty/cfg.py b/copyparty/cfg.py index 6152beb6..40b325cf 100644 --- a/copyparty/cfg.py +++ b/copyparty/cfg.py @@ -91,6 +91,7 @@ def vf_vmap() -> dict[str, str]: "forget_ip", "hsortn", "html_head", + "html_head_s", "lg_sbf", "md_sbf", "lg_sba", @@ -303,6 +304,7 @@ flagcats = { "hsortn": "number of sort-rules to add to media URLs", "unlist": "dont list files matching REGEX", "html_head=TXT": "includes TXT in the , or @PATH for file at PATH", + "html_head_s=TXT": "additional static text in the html ", "tcolor=#fc0": "theme color (a hint for webbrowsers, discord, etc.)", "nodirsz": "don't show total folder size", "du_who=all": "show disk-usage info to everyone", diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 6664c861..576130ad 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -286,10 +286,9 @@ class HttpCli(object): zs += "&" if "?" in zs else "?" ka["js"] = zs - zso = self.vn.flags.get("html_head") - if zso: + if "html_head_d" in self.vn.flags: ka["this"] = self - self._build_html_head(zso, ka) + self._build_html_head(ka) ka["html_head"] = self.html_head return tpl.render(**ka) # type: ignore @@ -758,9 +757,11 @@ class HttpCli(object): self.s.settimeout(self.args.s_tbody or None) if "norobots" in vn.flags: - self.html_head += META_NOBOTS self.out_headers["X-Robots-Tag"] = "noindex, nofollow" + if "html_head_s" in vn.flags: + self.html_head += vn.flags["html_head_s"] + try: cors_k = self._cors() if self.mode in ("GET", "HEAD"): @@ -931,8 +932,8 @@ class HttpCli(object): no304 = self.cookies.get("no304") return no304 == "y" or (self.args.no304 == 2 and no304 != "n") - def _build_html_head(self, maybe_html: Any, kv: dict[str, Any]) -> None: - html = str(maybe_html) + def _build_html_head(self, kv: dict[str, Any]) -> None: + html = str(self.vn.flags["html_head_d"]) is_jinja = html[:2] in "%@%" if is_jinja: html = html.replace("%", "", 1) @@ -5032,10 +5033,9 @@ class HttpCli(object): zs += "&" if "?" in zs else "?" targs["js"] = zs - zfv = self.vn.flags.get("html_head") - if zfv: + if "html_head_d" in self.vn.flags: targs["this"] = self - self._build_html_head(zfv, targs) + self._build_html_head(targs) targs["html_head"] = self.html_head zs = template.render(**targs).encode("utf-8", "replace") diff --git a/copyparty/up2k.py b/copyparty/up2k.py index 70729176..95754569 100644 --- a/copyparty/up2k.py +++ b/copyparty/up2k.py @@ -1142,7 +1142,7 @@ class Up2k(object): ft = "\033[0;32m{}{:.0}" ff = "\033[0;35m{}{:.0}" fv = "\033[0;36m{}:\033[90m{}" - zs = "bcasechk du_iwho ext_th_d html_head put_name2 mv_re_r mv_re_t rm_re_r rm_re_t srch_re_dots srch_re_nodot zipmax zipmaxn_v zipmaxs_v" + zs = "bcasechk du_iwho ext_th_d html_head html_head_d html_head_s put_name2 mv_re_r mv_re_t rm_re_r rm_re_t srch_re_dots srch_re_nodot zipmax zipmaxn_v zipmaxs_v" fx = set(zs.split()) fd = vf_bmap() fd.update(vf_cmap()) diff --git a/copyparty/web/browser.html b/copyparty/web/browser.html index 3b5c89ba..ebf41f8e 100644 --- a/copyparty/web/browser.html +++ b/copyparty/web/browser.html @@ -9,7 +9,7 @@ -{{- html_head }} +{{ html_head }} {%- if css %} {%- endif %} diff --git a/copyparty/web/idp.html b/copyparty/web/idp.html index 8a58f6be..f6e71d63 100644 --- a/copyparty/web/idp.html +++ b/copyparty/web/idp.html @@ -10,7 +10,7 @@ -{{- html_head }} +{{ html_head }} diff --git a/copyparty/web/md.html b/copyparty/web/md.html index 429a1eff..1dfed659 100644 --- a/copyparty/web/md.html +++ b/copyparty/web/md.html @@ -9,7 +9,7 @@ {%- if edit %} {%- endif %} -{{- html_head }} +{{ html_head }}
diff --git a/copyparty/web/mde.html b/copyparty/web/mde.html index ac3377c6..f6d0940f 100644 --- a/copyparty/web/mde.html +++ b/copyparty/web/mde.html @@ -8,7 +8,7 @@ -{{- html_head }} +{{ html_head }}
diff --git a/copyparty/web/msg.html b/copyparty/web/msg.html index 0b966d11..4ccac31f 100644 --- a/copyparty/web/msg.html +++ b/copyparty/web/msg.html @@ -8,7 +8,7 @@ -{{- html_head }} +{{ html_head }} diff --git a/copyparty/web/rups.html b/copyparty/web/rups.html index fe344c12..2cc0443a 100644 --- a/copyparty/web/rups.html +++ b/copyparty/web/rups.html @@ -10,7 +10,7 @@ -{{- html_head }} +{{ html_head }} diff --git a/copyparty/web/shares.html b/copyparty/web/shares.html index de128ab1..8808ed66 100644 --- a/copyparty/web/shares.html +++ b/copyparty/web/shares.html @@ -10,7 +10,7 @@ -{{- html_head }} +{{ html_head }} diff --git a/copyparty/web/splash.html b/copyparty/web/splash.html index 2beb433b..e98d5f63 100644 --- a/copyparty/web/splash.html +++ b/copyparty/web/splash.html @@ -9,7 +9,7 @@ -{{- html_head }} +{{ html_head }} diff --git a/copyparty/web/svcs.html b/copyparty/web/svcs.html index f63e670f..f07a8fb6 100644 --- a/copyparty/web/svcs.html +++ b/copyparty/web/svcs.html @@ -10,7 +10,7 @@ -{{- html_head }} +{{ html_head }} diff --git a/docs/rice/README.md b/docs/rice/README.md index 9f59485f..3d3b7cfe 100644 --- a/docs/rice/README.md +++ b/docs/rice/README.md @@ -64,6 +64,8 @@ if you give it the value `@ASDF` it will try to open a file named ASDF and send if the value starts with `%` it will assume a jinja2 template and expand it; the template has access to the `HttpCli` object through a property named `this` as well as everything in `j2a` and the stuff added by `self.j2s`; see [browser.html](https://github.com/9001/copyparty/blob/hovudstraum/copyparty/web/browser.html) for inspiration or look under the hood in [httpcli.py](https://github.com/9001/copyparty/blob/hovudstraum/copyparty/httpcli.py) +there is also `--html-head-s` and volflag `html_head_s` to add a plain static bit of text, possibly in addition to `--html-head` + # translations diff --git a/tests/util.py b/tests/util.py index f8448474..6bcbb9d7 100644 --- a/tests/util.py +++ b/tests/util.py @@ -164,7 +164,7 @@ class Cfg(Namespace): ex = "ctl_re db_act forget_ip idp_cookie idp_store k304 loris no304 nosubtle qr_pin qr_wait re_maxage rproxy rsp_jtr rsp_slp s_wr_slp snap_wri theme themes turbo u2ow zipmaxn zipmaxs" ka.update(**{k: 0 for k in ex.split()}) - ex = "ah_alg bname chdir chmod_f chpw_db doctitle df exit favico ipa html_head idp_login idp_logout lg_sba lg_sbf log_fk md_sba md_sbf name og_desc og_site og_th og_title og_title_a og_title_v og_title_i opds_exts shr tcolor textfiles txt_eol unlist vname xff_src zipmaxt R RS SR" + ex = "ah_alg bname chdir chmod_f chpw_db doctitle df exit favico ipa html_head html_head_d html_head_s idp_login idp_logout lg_sba lg_sbf log_fk md_sba md_sbf name og_desc og_site og_th og_title og_title_a og_title_v og_title_i opds_exts shr tcolor textfiles txt_eol unlist vname xff_src zipmaxt R RS SR" ka.update(**{k: "" for k in ex.split()}) ex = "ban_403 ban_404 ban_422 ban_pw ban_pwc ban_url spinner"