Allow html tags that contain only spaces

Open
#155 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start by locating the chomp function described in the issue and reproduce how markdownify handles . Verify that the conversion preserves the space between surrounding words without changing the existing handling of leading and trailing spaces; the issue does not name a test file.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
2.2k
Forks
203
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matthewwithanm/python-markdownify

All issues in matthewwithanm/python-markdownify

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.