From 6e6d3f53caa937d38f31f5a31dfbbe5d6b73eb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilhem=20Faur=C3=A9?= Date: Fri, 16 Jun 2023 15:51:18 +0200 Subject: [PATCH] fix : replace correctly every links of article instead of just the first --- spip2md/extended_models.py | 23 +++++++++-------------- spip2md/regexmaps.py | 4 ++-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/spip2md/extended_models.py b/spip2md/extended_models.py index 9d067a2..bb4dd4f 100644 --- a/spip2md/extended_models.py +++ b/spip2md/extended_models.py @@ -37,9 +37,7 @@ from spip2md.regexmaps import ( DOCUMENT_LINK, HTMLTAGS, IMAGE_LINK, - IMAGE_REPL, ISO_UTF, - LINK_REPL, MULTILANG_BLOCK, SECTION_LINK, SPECIAL_OUTPUT, @@ -406,35 +404,32 @@ class SpipRedactional(SpipWritable): return ( self._link_types[self._type_cursor][self._link_cursor], self._obj_getters[self._type_cursor], - IMAGE_REPL if self._type_cursor == 0 else LINK_REPL, + "!" if self._type_cursor == 0 else "", ) - for link, getobj, repl in LinkMappings(): + for link, getobj, prepend in LinkMappings(): # LOG.debug(f"Looking for {link} in {text}") for m in link.finditer(text): LOG.debug(f"Found internal link {m.group()} in {self._url_title}") try: - LOG.debug(f"Searching for object of id {m.group(2)} with {getobj}") + LOG.debug( + f"Searching for object of id {m.group(2)} with " + + getobj.__name__ + ) o: "Document | Article | Section" = getobj(int(m.group(2))) # TODO get full relative path for sections and articles # TODO rewrite links markup (bold/italic) after stripping if len(m.group(1)) > 0: - try: - repl = repl.format( - m.group(1).strip("{}"), o.dest_filename() - ) - except KeyError as err: - print(repl, m.group(1), o.dest_filename()) - raise err + repl = f"{prepend}[{m.group(1)}]({o.dest_filename()})" else: - repl = repl.format(o._storage_title, o.dest_filename()) + repl = f"{prepend}[{o._storage_title}]({o.dest_filename()})" LOG.debug( f"Translate link {m.group()} to {repl} in {self._url_title}" ) text = text.replace(m.group(), repl) except DoesNotExist: LOG.warn(f"No object for link {m.group()} in {self._url_title}") - text = text.replace(m.group(), repl.format("", "NOT FOUND"), 1) + text = text.replace(m.group(), prepend + "[](NOT FOUND)", 1) return text # Get this object url, or none if it’s the same as directory diff --git a/spip2md/regexmaps.py b/spip2md/regexmaps.py index 8941d9c..1510afb 100644 --- a/spip2md/regexmaps.py +++ b/spip2md/regexmaps.py @@ -159,8 +159,8 @@ SECTION_LINK = ( compile(r"\[(.*?)\]\((?:rub|rubrique)([0-9]+)(?:\|(.*?))?\)", S | I), ) -LINK_REPL = r"[{}]({})" # Name and path can be further replaced with .format() -IMAGE_REPL = r"![{}]({})" # Name and path can be further replaced with .format() +# LINK_REPL = r"[{}]({})" # Name and path can be further replaced with .format() +# IMAGE_REPL = r"![{}]({})" # Name and path can be further replaced with .format() # Multi language block, to be further processed per lang