[PATCH v2 14/15] md2html-add-asset-postfix-arg

John Keeping john at keeping.me.uk
Mon Jun 18 21:21:36 CEST 2018


On Mon, Jun 18, 2018 at 10:58:26AM +0800, Andy Green wrote:
> Extend md2html with a third argument for URL postfix, like "?h=mybranch"
> 
> Signed-off-by: Andy Green <andy at warmcat.com>
> ---
>  filters/html-converters/md2html |   17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/filters/html-converters/md2html b/filters/html-converters/md2html
> index eb5d977..128a61b 100755
> --- a/filters/html-converters/md2html
> +++ b/filters/html-converters/md2html
> @@ -9,31 +9,36 @@ from urllib.parse import urljoin
>  
>  class AssetMappingProcessor(markdown.treeprocessors.Treeprocessor):
>  
> -    def __init__(self, asset_prefix):
> +    def __init__(self, asset_prefix, asset_postfix):
>          self.asset_prefix = asset_prefix
> +        self.asset_postfix = asset_postfix
>  
>      def run(self, root):
>          asset_prefix = self.asset_prefix
> +        asset_postfix = self.asset_postfix
>          for img in root.iter('img'):
>              src = img.get('src')
>              if src is None:
>                  continue
> -            img.set('src', urljoin(asset_prefix, src))
> +            img.set('src', urljoin(urljoin(asset_prefix, src), asset_postfix))
>  
>  
>  class AssetMappingExtension(markdown.extensions.Extension):
>  
>      def __init__(self, **kwargs):
> -        self.config = {'asset_prefix': ['', 'prefix for relative asset URLs']}
> +        self.config = {'asset_prefix': ['', 'prefix for relative asset URLs'], 'asset_postfix': ['', 'postfix for relative asset URLs']}

For style it would be nice to align this under asset_prefix.

>          super(AssetMappingExtension, self).__init__(**kwargs)
>  
>      def extendMarkdown(self, md, md_globals):
>          asset_prefix = self.getConfig('asset_prefix')
>          if not asset_prefix:
>              return
> +        asset_postfix = self.getConfig('asset_postfix')
> +        if not asset_postfix:
> +            return

Is this right?  Should we allow one of these to be empty and still
process the other one?  In other words, shouldn't the bail out condition
be:

    if not (asset_prefix or asset_postfix):
        return

I don't think any change to AssetMappingProcessor is required because
urljoin already does the right thing when handed the empty string and
the config assure that if no value is specified then that is what we
get.

>  
>          md.treeprocessors.add('asset_mapping',
> -                              AssetMappingProcessor(asset_prefix),
> +                              AssetMappingProcessor(asset_prefix, asset_postfix),
>                                '_end')
>  
>  
> @@ -333,8 +338,8 @@ extension_configs = {
>      "markdown.extensions.codehilite":{"css_class":"highlight"}
>  }
>  
> -if len(sys.argv) > 2:
> -    extensions.append(AssetMappingExtension(asset_prefix=sys.argv[2]))
> +if len(sys.argv) > 3:
> +    extensions.append(AssetMappingExtension(asset_prefix=sys.argv[2],asset_postfix=sys.argv[3]))

Can we allow specifying only the prefix here?  Something like:

    if len(sys.argv) > 2:
        args = {'asset_prefix': sys.argv[2]}
        if len(sys.argv) > 3:
            args['asset_postfix'] = sys.argv[3]
        extensions.append(AssetMappingExtension(**args))

>  
>  # Note: you may want to run this through bleach for sanitization
>  markdown.markdownFromFile(output_format="html5", extensions=extensions, extension_configs=extension_configs)
> 


More information about the CGit mailing list