-
Notifications
You must be signed in to change notification settings - Fork 871
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
Add support to specify table class with attr_list #312
Comments
@jonblack I have two responses to your request. First, see my position on adding features to the existing table extension as explained in issue #74. My position hasn't changed. In fact, there are a few third-party extensions which implement more feature rich table syntax as listed on the wiki. That said, seems to me like raw html will give you the most power to best meet your needs. Second, even if I was interested in adding better support for attr_list to the existing table extensions, how would one differentiate between attributes defined on a row, or on a table. The syntax only defines the cells, the rows and table are inferred. The same issue exists with lists as explained in issue #227. My position here is no different that with lists. But again, a third party extension is certainly welcome to offer a solution. That said, raw html is the only solution I'd be interested in. |
Thanks for the detailed response.
I already found that in a response to another issue and agree with your decision not to expand the tables extension.
There are two ways of doing this. The first and most complex way is, from a syntax perspective, shown in the example below:
I'll admit, this is extremely verbose and does remove some of the tidiness you get using markdown-styled tables. At the same time, at least it removes the need for html. The second way is allow the table .my-custom-table-class {
...
}
table .my-custom-table-class > tr {
...
} I think this offers enough flexibility and I'd assume is trivial to implement. I see you closed the issue. Hopefully I've given you something to think about. Regardless, thanks for the excellent library. |
I've just encountered this issue with python-markdown today and I must say - it's insanely frustrating that I can't apply a style inline to a markdown table using attr_list. After several hours of working with python-markdown forks and python-markdown third party extensions (with nothing working), the only solutions I see left are:
I assume that you've not changed your position on this feature, but I'd very much like to +1 this feature request. It seems trivial to implement and highly desired. |
So I got here via the I would like to propose a solution that is backwards compatible and handles all levels of nested HTML constructs. The basic idea is to add a new name/value pair to an attribute list:
This would product a styled list: If the target is left out, the current behavior applies. This is critical to provide backwards compatibility. Now, to handle the case where I want to style list items and the list itself, we add the ability to have multiple attribute lists:
is the same as:
Since the default is current behavior, the following should do what you expect:
For simplicity, if the value gets redefined, it could either be "last definition wins", "additive" or a syntax error. It really doesn't matter, as long as it is defined:
could end up with Finally, what happens if you supply a target that doesn't exist?
Again, this could either silently ignore such classes or it could be an error. I would lean towards an error so people get notified when something goes wrong. Tables would behave the same way, except valid targets could be
Would you accept such a syntax if I were to work on it? |
@SoftwareMaven thanks for your detailed proposal. However I am not interested. Of course, you are free to implement a third party extension which implements whatever syntax you would like. If your extensions become widely popular, then perhaps I might reconsider. That said, I think you have a decent start on a proposal. With the assumption that you will be pursuing a third party extension, here is my feedback: Given you example:
Block level attribute lists are always at the end of the block, so shouldn't the Regarding the actual syntax, I think use of the What I don't like is the "target=some-thing-that-is-only-implied-in-the-document" part. Maybe something simpler like Another possibility would be to use CSS style selectors (see some interesting suggestions) at the Either way, I don't like it as applies to a construct that does not exist in the Markdown document. Sorry, I just can't get past that point. |
just add a default css tag. Pretty simple. class=markdown-table problem solved. |
Can you please elaborate on how and where can I add a default class to generated HTML elements? I couldn't find any such property or attribute in the |
For anyone that's looking for a quick and dirty solution to this, I have implemented an extension for myself which mirrors the Kramdown syntax (because I am having to render markdown that's been written previously for Jekyll). Syntax is: for lists
for tables
Caveat: I wrote this in about an hour, as my first ever attempt at a markdown extension. It works for what I needed it for, but I have not tested it properly by any means. YMMV import markdown
import re
from markdown.extensions import attr_list
def makeExtension(**kwargs):
return ImpliedAttrListExtension(**kwargs)
class ImpliedAttrListExtension(markdown.Extension):
"""Extension for attatching `attr_list` entries to implied elements. Specifically: lists and tables"""
def extendMarkdown(self, md: markdown.Markdown, *args, **kwargs):
md.preprocessors.register(ImpliedAttrListPreprocessor(md), "implied_attr_list", 100)
md.treeprocessors.register(ImpliedAttrListTreeprocessor(md), 'implied_attr_list', 100)
md.registerExtension(self)
class ImpliedAttrListPreprocessor(markdown.preprocessors.Preprocessor):
def run(self, lines):
"""
Insert a blank line in between the declaration of the attr_list and the thing that it applies to
This will allow it to render the list normally. The attr_list will get rendered into the text of a paragraph
tag which the Treeprocessor below will handle
"""
new_lines = []
for line in lines:
new_lines.append(line)
if re.fullmatch(ImpliedAttrListTreeprocessor.BASE_RE, line):
new_lines.append("")
return new_lines
class ImpliedAttrListTreeprocessor(attr_list.AttrListTreeprocessor):
def run(self, doc):
"""
Iterate through the doc, locating <p> tags that contain ONLY the syntax for attr_lists.
Once one is found, the value is applied to the next element in the iteration of the doc, and the
<p> tag is removed
:param doc:
:return:
"""
holdover = None
removes = []
for elem in doc.iter():
if holdover is not None:
self.assign_attrs(elem, holdover)
holdover = None
if elem.tag in ["p"] and elem.text is not None:
m = re.fullmatch(self.BASE_RE, elem.text)
if m:
holdover = m.group(1)
removes.append(elem)
if len(removes) > 0:
parent_map = {c: p for p in doc.iter() for c in p}
for r in removes:
parent = parent_map[r]
parent.remove(r) |
The bootstrap 3 framework has support for tables, but requires various classes be set on the
<table>
element: e.g.<table class="table table-bordered table-striped">
.If I try to do this with the
attr_list
andtables
extensions, a new empty row is created and the class is applied to the first<td>
element in that row.For example...
...outputs...
Since
<table>
is a block level element, I would expect the attributes to be applied to there; I certainly don't expect an extra empty row.The text was updated successfully, but these errors were encountered: