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

Inline Styling Gets Messy #156

Open
jrg94 opened this issue Sep 10, 2023 · 1 comment
Open

Inline Styling Gets Messy #156

jrg94 opened this issue Sep 10, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@jrg94
Copy link
Member

jrg94 commented Sep 10, 2023

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!

@jrg94 jrg94 added the bug Something isn't working label Sep 10, 2023
@jrg94
Copy link
Member Author

jrg94 commented Sep 10, 2023

A dream of mine would be to borrow the f-string syntax somehow, so users basically would never have to touch the Inline object. For example, something like this would be wonderful:

doc.add_paragraph(f"{'How Now':italics} {'Brown':italics&bold} {'Cow':italics}")

And in fact, Inline could be embedded in f-strings, but the Inline element would be lost completely. Hmmm....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant