简体   繁体   中英

How to create a new markdown cue extending another one using python-markdown

Im using markdown for pelican, but the control over the images is not very good (just like with the rest of markdown)

Im trying to create an extenxion to wrap the image one with some width to streach them and some tag around them.

Basically I want to write this:

!![alt text](path.to.image.png title of the image)

and generate this html

<a href="path.to.image.png"><img width=100% src="path.to.image.png" alt="alt text" title="title of the image"/></a>

The tutorial is too basic. Is it possible to wrap around the current img code or do i have to create it like is a brand new md cue

thanks!

Yes, you would need to create your own inline pattern. None of the built-in patterns will work for that. That said, it shouldn't be to hard:

class MyImgPattern(Pattern):
    def handleMatch(self, m):
        a = etree.Element('a', attrib={'href':m.group(3)})
        img = etree.Element('img', attrib={
            'width': '100%', 
            'src': m.group(3),
            'alt': m.group(2),
            'title': m.group(4)
        })
        a.append(img)
        return a

Note that I'm assuming a certain structure to your regular expression, based on the one used by the built-in image pattern. Depending on the regex you use, you may need to adjust the groups accordingly.

Then just tell Markdown about your new pattern as per normal and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM