From 4ec155d70d810ee4cb0d6a825fdae4eb1515ff40 Mon Sep 17 00:00:00 2001 From: Filipp Date: Tue, 28 Mar 2023 11:00:26 +0300 Subject: [PATCH] WIP fixing relative links check the FIXMEs --- src/generator/lib/util.py | 28 +++++++++++++++++++++ src/generator/templates/api/lib/rbuild.mako | 6 ++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/generator/lib/util.py b/src/generator/lib/util.py index 0ddc7616b1..bcc5b193d7 100644 --- a/src/generator/lib/util.py +++ b/src/generator/lib/util.py @@ -1,6 +1,7 @@ import os import re import subprocess +import urllib import inflect from dataclasses import dataclass @@ -1173,6 +1174,33 @@ def string_impl(p): "string": lambda x: x }.get(p.get("format", p["type"]), lambda x: f"{x}.to_string()") +MD_LINKS_CAPTURE = re.compile(r''' + \[ # Opening bracket for the "name" part of the url + ([^]]+?) # Some amount of non-closing bracket `]` symbols, + # Captured in a group + \] # Close the name part + \s* # maybe a whitespace + \( # Opening a paren for the url part + ([^)]+?) # Url, captured in a group + \) # closing paren +''', re.VERBOSE) + +# FIXME: combine with links handling in .docs +def fix_relative_links(url_base): + url_parsed = urllib.parse.urlparse(url_base) + url_base = f"{url_parsed.scheme}://{url_parsed.netloc}" + def replace_url(url_match: re.match): + original_url = url_match.group(0) + name = url_match.group(1) + url = url_match.group(2) + if url.startswith('/'): + return f"[{name}]({url_base}{url})" + return original_url + + def fixer(docs): + # FIXME: replace with proper markdown parser and documentationLink value + return MD_LINKS_CAPTURE.sub(replace_url, docs) + return fixer if __name__ == '__main__': raise AssertionError('For import only') diff --git a/src/generator/templates/api/lib/rbuild.mako b/src/generator/templates/api/lib/rbuild.mako index 696f3e6b66..037a08beab 100644 --- a/src/generator/templates/api/lib/rbuild.mako +++ b/src/generator/templates/api/lib/rbuild.mako @@ -6,7 +6,7 @@ rust_copy_value_s, organize_params, REQUEST_VALUE_PROPERTY_NAME, build_all_params, rb_type_params_s, hub_type_params_s, mb_type_params_s, mb_additional_type_params, struct_type_bounds_s, METHODS_RESOURCE, SPACES_PER_TAB, prefix_all_but_first_with, - METHODS_BUILDER_MARKER_TRAIT, remove_empty_lines, method_default_scope, rust_doc_sanitize) + METHODS_BUILDER_MARKER_TRAIT, remove_empty_lines, method_default_scope, rust_doc_sanitize, fix_relative_links) %>\ <%namespace name="util" file="../../../lib/util.mako"/>\ <%namespace name="lib" file="lib.mako"/>\ @@ -80,7 +80,7 @@ impl${rb_params} ${ThisType} { % if 'description' in m: /// Create a builder to help you perform the following task: /// - ${m.description | rust_doc_sanitize, rust_doc_comment, indent_all_but_first_by(1)} + ${m.description | fix_relative_links(documentationLink), rust_doc_sanitize, rust_doc_comment, indent_all_but_first_by(1)} % endif % if required_props: /// @@ -91,7 +91,7 @@ impl${rb_params} ${ThisType} { arg_prefix = "/// * `" + p.name + "` - " %>\ ${arg_prefix}${p.get('description', "No description provided.") - | remove_empty_lines, prefix_all_but_first_with(' ' * SPACES_PER_TAB + '///' + ' ' * (len(arg_prefix) - len('///')))} + | fix_relative_links(documentationLink), remove_empty_lines, prefix_all_but_first_with(' ' * SPACES_PER_TAB + '///' + ' ' * (len(arg_prefix) - len('///')))} % endfor % endif pub fn ${mangle_ident(a)}${type_params}(&self${method_args}) -> ${RType}${mb_tparams} {