original_docstring: 
        Rewrites markdown so that relative links that were written at
        source_path will still work when inserted into a file at destination_path
      
docstring: Rewrites markdown so that relative links that were written at
  source_path will still work when inserted into a file at destination_path
docstring_tokens: Rewrites markdown so that relative links that were written at source_path will still work when inserted into a file at destination_path
code: def rewrite_relative_urls(
      markdown: str, source_path: Path, destination_path: Path
  ) -> str:
      '''
        Rewrites markdown so that relative links that were written at
        source_path will still work when inserted into a file at destination_path
      '''
      def rewrite_url(url: str) -> str:
          scheme, netloc, path, params, query, fragment = urlparse(url)
  
          is_absolute = path.startswith('/')
          if is_absolute:
              return url
  
          trailing_slash = path.endswith('/')
  
          path = os.path.relpath(source_path.parent / path,
                                 destination_path.parent)
  
          # ensure forward slashes are used, on Windows
          path = Path(path).as_posix()
  
          if trailing_slash:
              # the above operation removes a trailing slash. Add it back if it
              # was present in the input
              path = path + '/'
  
          return urlunparse((scheme, netloc, path, params, query, fragment))
  
      def found_href(m: 're.Match', url_group_index=-1) -> str:
          match_start, match_end = m.span(0)
          href = m.group(url_group_index)
          href_start, href_end = m.span(url_group_index)
          rewritten_url = rewrite_url(href)
          return (
              m.string[match_start:href_start]
              + rewritten_url
              + m.string[href_end:match_end]
          )
  
      markdown = re.sub(
          MARKDOWN_LINK_REGEX,
          partial(found_href, url_group_index=3),
          markdown
      )
      markdown = re.sub(
          MARKDOWN_IMAGE_REGEX,
          partial(found_href, url_group_index=3),
          markdown
      )
      markdown = re.sub(
          MARKDOWN_LINK_DEFINITION_REGEX,
          partial(found_href, url_group_index=2),
          markdown
      )
  
      return markdown
code_tokens: def rewrite_relative_urls ( markdown : str , source_path : Path , destination_path : Path ) -> str : def rewrite_url ( url : str ) -> str : scheme , netloc , path , params , query , fragment = urlparse ( url ) is_absolute = path . startswith ( '/' ) if is_absolute : return url trailing_slash = path . endswith ( '/' ) path = os . path . relpath ( source_path . parent / path , destination_path . parent ) path = Path ( path ) . as_posix ( ) if trailing_slash : path = path + '/' return urlunparse ( ( scheme , netloc , path , params , query , fragment ) ) def found_href ( m : 're.Match' , url_group_index = - 1 ) -> str : match_start , match_end = m . span ( 0 ) href = m . group ( url_group_index ) href_start , href_end = m . span ( url_group_index ) rewritten_url = rewrite_url ( href ) return ( m . string [ match_start : href_start ] + rewritten_url + m . string [ href_end : match_end ] ) markdown = re . sub ( MARKDOWN_LINK_REGEX , partial ( found_href , url_group_index = 3 ) , markdown ) markdown = re . sub ( MARKDOWN_IMAGE_REGEX , partial ( found_href , url_group_index = 3 ) , markdown ) markdown = re . sub ( MARKDOWN_LINK_DEFINITION_REGEX , partial ( found_href , url_group_index = 2 ) , markdown ) return markdown
short_docstring: Rewrites markdown so that relative links that were written at
  source_path will still work when inserted into a file at destination_path
short_docstring_tokens: Rewrites markdown so that relative links that were written at source_path will still work when inserted into a file at destination_path
comment: '''
        Rewrites markdown so that relative links that were written at
        source_path will still work when inserted into a file at destination_path
      '''$SEP$# ensure forward slashes are used, on Windows$SEP$# the above operation removes a trailing slash. Add it back if it$SEP$# was present in the input