Skip to content

Inline Styling Gets Messy #156

@jrg94

Description

@jrg94

Alright, I was working on trying to get the following text to generate using SnakeMD:

There are 100 points on the exam. Please write your answers on the test sheets, and of course don’t forget to write and sign your name on the top sheet. Consider the space allotted as an indication of the expected length of the answer. Summaries of relevant Javadoc APIs are included in the handout. Please do not write on the handout , and return it with your exam. If you need to look up the Javadoc API for anything else or for more detail, your instructor will have a browser open on a computer in the classroom for you to use.

With the following code:

doc.add_block(
    snakemd.Paragraph([
        snakemd.Inline(
            'There are 100 points on the exam.  Please write your answers on the test sheets, and of course don’t forget to write and sign your name on the top sheet. Consider the space allotted as an indication of the expected length of the answer. Summaries of relevant Javadoc APIs are included in the handout. Please '
        ).italicize(),
        snakemd.Inline(
            'do not write on the handout '
        ).italicize().bold(),
        snakemd.Inline(
            ', and '
        ).italicize(),
        snakemd.Inline(
            'return it '
        ).italicize().bold(),
        snakemd.Inline(
            'with your exam.  If you need to look up the Javadoc API for anything else or for more detail, your instructor will have a browser open on a computer in the classroom for you to use.'
        ).italicize()
    ])
)

In the original design, I figured we would defer as much of the structure as possible to the user. This runs perfectly in line with the way folks already format their strings using concatenation. In other words, users would be expected to get their spacing right, just as you would with something like: "Hello, " + input("What is your name?").

The problem is that when styles are applied, they are applied to the individual inline units and there are some nastier bugs that can unexpectedly occur. For example, the code above produces the following nightmare:

_There are 100 points on the exam. Please write your answers on the test sheets, and of course don’t forget to write and sign your name on the top sheet. Consider the space allotted as an indication of the expected length of the answer. Summaries of relevant Javadoc APIs are included in the handout. Please **do not write on the handout **, and __**return it **_with your exam. If you need to look up the Javadoc API for anything else or for more detail, your instructor will have a browser open on a computer in the classroom for you to use.

To me, the whitespace should not affect the output but it seemingly does. Therefore, the correct code would be:

doc.add_block(
    snakemd.Paragraph([
        snakemd.Inline(
            'There are 100 points on the exam.  Please write your answers on the test sheets, and of course don’t forget to write and sign your name on the top sheet. Consider the space allotted as an indication of the expected length of the answer. Summaries of relevant Javadoc APIs are included in the handout. Please '
        ).italicize(),
        snakemd.Inline(
            'do not write on the handout'
        ).italicize().bold(),
        snakemd.Inline(
            ' , and '
        ).italicize(),
        snakemd.Inline(
            'return it'
        ).italicize().bold(),
        snakemd.Inline(
            ' with your exam.  If you need to look up the Javadoc API for anything else or for more detail, your instructor will have a browser open on a computer in the classroom for you to use.'
        ).italicize()
    ])
)

In other words, anywhere we are applying both italics and bold would need to be free of trailing and leading spaces. I believe the reason this ends up being an issue is because adjacent Inline elements have italics which cause problems when running into each other.

Ultimately, I hate this because it's not something I want the users to have to worry about. They shouldn't have to really even know markdown to use the library, but they'll be stuck solving markdown bugs as a result.

Now, the solution would be applying italics to the entire Paragraph and bold to the key elements. Or in a broader sense, SnakeMD should maybe have the capability to aggregate repeated styles. Something to think about for sure!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions