Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow html tags that contain only spaces #155

Open
tthkbw opened this issue Oct 27, 2024 · 2 comments
Open

Allow html tags that contain only spaces #155

tthkbw opened this issue Oct 27, 2024 · 2 comments

Comments

@tthkbw
Copy link

tthkbw commented Oct 27, 2024

markdownify converts this:

one<i> </i>two

into:

oneone

because the function chomp returns the text inside the tags as an empty string.

One can argue that empty tags like this should not be allowed, but the epub The Last Dangerous Visions from Blackstone Publishing has this structure in dozens of locations and the space inside the tag is the space between two words of the text. This results in my epub reader showing a bunch of words without spaces between them when I use markdownify to convert the text of the epub files into markdown.

I don't pretend to understand how the markdownify code works in detail, but modifying chomp to look like this fixes the issue for me:

def chomp(text):
    """
    If the text in an inline tag like b, a, or em contains a leading or trailing
    space, strip the string and return a space as suffix of prefix, if needed.
    This function is used to prevent conversions like
        <b> foo</b> => ** foo**
    """
    #change to allow empty tags: "<i> </i>" and maintain the space
    if text.isspace():
        prefix = ''
        suffix = ''
        text = ' '
    else:
        prefix = ' ' if text and text[0] == ' ' else ''
        suffix = ' ' if text and text[-1] == ' ' else ''
        text = text.strip()
    return (prefix, suffix, text)

An unfortunate side effect of my fix is that this turns into something similar to &nbsp.

@AlexVonB
Copy link
Collaborator

Hey Terry, thanks for your issue! Unfortunately, your code converts <b> </b> to ** ** which is not what should happen. Maybe you could preprocess your ebook by replacing all occurances of <i> </i> with a single space?

@chrispy-snps
Copy link
Collaborator

chrispy-snps commented Jan 2, 2025

Perhaps there could be a combined fix for #175 and this issue. Let's see where that discussion leads first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants