Skip to content

Admonition: add the possibility to specify additional CSS classes names#580

Closed
WhiteWinterWolf wants to merge 6 commits intoPython-Markdown:masterfrom
WhiteWinterWolf:master
Closed

Admonition: add the possibility to specify additional CSS classes names#580
WhiteWinterWolf wants to merge 6 commits intoPython-Markdown:masterfrom
WhiteWinterWolf:master

Conversation

@WhiteWinterWolf
Copy link
Contributor

Hi,
I read about the feature freeze in the other pull requests, so feel free to tell me if there is a better location to propose this patch.

I need to be able to add additional classes to admonition, typically to make some box floating on the right while keeping the other ones in full width. I don't think there is a way to make the Admonition extension to work with the Attribute List to achieve this, so I updated Admonition to add this feature.

Additional CSS classes names can now be appended to the admonition name using dots as a separator.

The following markdown:

!!!note.floatright
    This is a floating note.

Now generates the following HTML code:

<div class="admonition note floatright">
    <p class="admonition-title">Note</p>
    <p>This is a floating note.</p>
</div>

Dots are currently invalid in admonition names and break the admonition box, so I think it is safe to assume that this change should not cause any compatibility issue as nobody should be already using such dotted syntax or expect any particular behavior, and dots are already used as class names separator in CSS files so this syntax seems natural to me.

Additional CSS classes names can be appended to the admonition name using dots as a separator.

The following markdown:

    !!!note.floatright
        This is a floating note.

Generates the following HTML code:

    <div class="admonition note floatright">
        <p class="admonition-title">Note</p>
        <p>This is a floating note.</p>
    </div>
Updated the regex to include the fix from the issue Python-Markdown#550.
@facelessuser
Copy link
Collaborator

From what I understand, if this were to be accepted, it most likely wouldn't be accepted until 3.0.

With that said, why use .? I get that it has its association with CSS, but we don't require the first class to start with .. To me it seems odd, but maybe that is just me. Why not use a space? Since the title is contained within quotes, I don't see why classes separated with spaces would be a problem unless there is something I overlooked.

@WhiteWinterWolf
Copy link
Contributor Author

@facelessuser Yes, since the quotes are mandatory even for single word titles then space should be technically fine (as would do comma, or pretty any other punctuation except the minus character).

I saw the space as an argument separator, where the first argument defines the CSS classes and the second one defines the title. If it seems more natural to have several space-separated arguments as classes except the latest one which gets a special meaning when surrounded by quotes and becomes a title instead of a CSS class, why not. This still seem a little error prone to me, but this is just my current feeling and I may be wrong.

Thinking in terms of arguments, another possibility would be to just require the quotes on multiple-words arguments both for the classes and the title, making these syntaxes legal:

!!! "note otherclass" "long title"
!!! note title
!!! "note" "title"
!!! note "long title"
!!! "note otherclass" title
!!! "note otherclass"

This may also feel natural and should be easy to implement (the class name is already mandatory and its value cannot contain quotes).

@facelessuser
Copy link
Collaborator

This still seem a little error prone to me, but this is just my current feeling and I may be wrong.

I don't personally see a problem with spaces. The title is very specific, and with the syntax being strict as it is in this regard, I don't see a problem with just spaces. But sure we could technically use any delimiter that doesn't make syntax evaluation difficult. I think . feels awkward (to me a least), but I'll let others weigh in on this. This is assuming the idea of multiple classes is generally accepted for merge.

Thinking in terms of arguments, another possibility would be to just require the quotes on multiple-words arguments both for the classes and the title, making these syntaxes legal:

You're probably going to have a harder time getting a rework of the syntax vs an expanding of the syntax approved. I don't think quoting the classes is a good idea as it will just make things more confusing.

@WhiteWinterWolf
Copy link
Contributor Author

WhiteWinterWolf commented Sep 8, 2017

@facelessuser : I have updated the patch with your suggestion to use spaces as class names separators.

Travis now fails but I have the impression that this is unrelated to my modification (checkspelling now fails on code_hilite.html).

@facelessuser
Copy link
Collaborator

I imagine Travis occasionally upgrades their Ubuntu and that sometimes upgrades aspell version being used. I would just add richeland to the dictionary: https://github.com/Python-Markdown/markdown/blob/master/.spell-dict. I know it's not related to what you are doing, but it will have to get updated anyways.

Most likely, if @waylan is okay with this moving forward, it will probably be required that you put in some unit tests to verify the new format. Also, I think currently admonitions takes the first class and makes that the title if no title is given? I think we'd need a confirmation from @waylan whether that is ideal, or if all provided classes should be taken into consideration. I think in general, the first seems okay to me. It is doubtful anyone would be spelling out a reasonable title with CSS classes :).

@facelessuser
Copy link
Collaborator

Thinking about this more, you can probably ignore the spelling issue. I don't know if/when this is getting merged, so I imagine the spelling will get fixed well before this gets merged.

@waylan waylan added this to the Version 3.0 milestone Sep 8, 2017
@waylan
Copy link
Member

waylan commented Sep 8, 2017

Initially I was going to say that I have no interest in this. But then I read through the rest of the discussion. Space separated classes with a quote wrapped title seem fine. And yes, if no title is provided, the first class should become the title. No backward compatibility issues for existing documents there.

That said, I'm not merging this until version 3.0.

@WhiteWinterWolf
Copy link
Contributor Author

@facelessuser @waylan: Thanks for your feedback, I've updated the unit tests and documentation to match the new feature. I'm fine with an inclusion in 3.0.


def get_class_and_title(self, match):
klass, title = match.group(1).lower(), match.group(2)
klass = re.sub(' +', ' ', klass)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I prefer (when possible) to pre-compile regex. This would be one that can easily be either compiled once per file or at least once per class instantiation (either is probably fine). This isn't really a big issue, but every little bit helps. Other than that, I have no complaints with anything else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes all reflex should be compiled as soon as possible.

@codecov
Copy link

codecov bot commented Sep 18, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@576e15e). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #580   +/-   ##
=========================================
  Coverage          ?   93.73%           
=========================================
  Files             ?       29           
  Lines             ?     2859           
  Branches          ?        0           
=========================================
  Hits              ?     2680           
  Misses            ?      179           
  Partials          ?        0
Impacted Files Coverage Δ
markdown/extensions/admonition.py 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 576e15e...731c8d9. Read the comment docs.


def makeExtension(*args, **kwargs):
return AdmonitionExtension(*args, **kwargs)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You broke linting with this extra line. There should only be one newline at the end of files, not two.

@facelessuser
Copy link
Collaborator

Thanks @WhiteWinterWolf, that covers all my complaints.

@WhiteWinterWolf
Copy link
Contributor Author

Hi @facelessuser,

Sorry for the inconvenience, vi default behavior and this time I went to quickly and forgot to remove it.

Nevertheless, I do not see how this would break Python. A quick check gives me leads to ESLint (for Java) which in fact changed their own rules from requiring exactly one linefeed characters (the issue we have here) to having at least one linefeed character, allowing more than one.

The actual issue that needs to be detected is indeed when there is no linefeed at the end of the file.

Nevertheless, the culprit character has been removed now (and yes, I know: "vi is the evil" ;) !).

@facelessuser
Copy link
Collaborator

Nevertheless, I do not see how this would break Python.

This is not a question of whether it will break Python. A lot of the checks done by flake8 are stylistic. It is perfectly acceptable for a project to enforce style guides it prefers whether or not they "break" compilation or parsing.

You can always run linting locally, before pushing, to avoid unexpected lint issues in the review.

@waylan
Copy link
Member

waylan commented Sep 18, 2017

Interesting side-effect of making this change is that I noticed for the first time that we have an incorrectly formatted admonition (see #583). The 'spellchecking' test is failing because of the GitHub username "richeland" not being in the spelling dictionary.

I was curios why that didn't fail any other tests and realized that the word only appears within the incorrectly formatted admonition. That admonition's title ("see also") was missing quotes, so the class was "see" with the body of the admonition being "also". The actual body was being rendered as a codeblock. And as the spellchecker skips codeblocks, it was not catching "richeland".

With this change, both "see" and "also" were being assigned as classes and the body was being properly identified. Therefore, for the first time, the body of the admonition was being checked by the 'spellchecking' test.

@facelessuser
Copy link
Collaborator

Ah, that makes sense. I was also wondering why it didn't fail on master and other pulls since this one. I had assumed there was an aspell update (as that has happened to me on other repos). But it makes sense as we are now handling the previously erroneous extra classes.

@waylan
Copy link
Member

waylan commented Jul 24, 2018

As this was originally created on the master branch, I couldn't easily add a remote to my local repo to clone it and rebase. Therefore, I've opened a new PR at #685 and this is being closed in favor of that.

@waylan waylan closed this Jul 24, 2018
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

Successfully merging this pull request may close these issues.

3 participants

Comments