Using Blocks Extension to parse YAML and build an HTML element #2585
-
Hello, My idea is to create a new Block Extension in order to parse a YAML file with the rubric definition and build a table. /// rubric
levels:
1: Excellent
2: Good
3: Acceptable
4: Poor
criteria:
- title: Clarity
description: The writing is clear and easy to understand.
levels:
1: The writing is excellent.
2: The writing is good.
3: The writing is acceptable.
4: The writing is poor.
- title: ...
/// As for now, I've managed to define a class Rubric(Block):
"""Figure captions."""
NAME = 'rubric'
CLASSES = ''
ARGUMENT = None
OPTIONS = {
'type': ['', type_html_identifier]
}
def __init__(self, length, tracker, md, config):
"""Initialize."""
super().__init__(length, tracker, md, config)
def on_markdown(self):
return "raw"
def on_create(self, parent):
"""Initialize."""
el = etree.SubElement(parent, 'table')
return el
class RubricExtension(BlocksExtension):
"""Rubric Extension."""
def __init__(self, *args, **kwargs):
"""Initialize."""
self.config = {}
super().__init__(*args, **kwargs)
def extendMarkdownBlocks(self, md, block_mgr):
"""Extend Markdown blocks."""
config = self.getConfigs()
block_mgr.register(Rubric, config)
def makeExtension(*args, **kwargs):
"""Return extension."""
return RubricExtension(*args, **kwargs) However, I'm having trouble reading the block content. I want to parse it and, then, construct the table. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Sorry, I meant to take a look sooner. Let me try to make sure I understand your question. You want to get all the content before the table is created? If this is true, you can only access the content in the Are you running into some kind of technical problem? |
Beta Was this translation helpful? Give feedback.
-
That's exactly what I did :) def on_end(self, block):
"""Finalize."""
self.parse_rubric(block.text)
block.text = ""
self.handle_levels(block)
self.handle_criteria(block)
return block
def parse_rubric(self, text):
"""Parse rubric."""
self.data = yaml.safe_load(text) Now, I'm running into a problem when I write an empty rubric Block. /// rubric
///
I don't really understand the internals of how Markdown Extensions and Block Extensions work, so I don't know why this error raises. I would appreciate a lot if you could give me some guidance. The code is available at https://github.com/joapuiib/pymdown-rubric-extension/blob/fix/empty_rubric/rubric/__init__.py (I can copy/paste it here if you prefer) Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Sorry, I meant to take a look sooner. Let me try to make sure I understand your question.
You want to get all the content before the table is created? If this is true, you can only access the content in the
on_end
hook. So, as documented, all the content will be stored in the element you gave it. You can then remove the content from he element, process it and stick it back however you like.Are you running into some kind of technical problem?