details.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Details.
  3. pymdownx.details
  4. MIT license.
  5. Copyright (c) 2017 Isaac Muse <isaacmuse@gmail.com>
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  7. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  9. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies or substantial portions
  11. of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  15. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  16. DEALINGS IN THE SOFTWARE.
  17. """
  18. from markdown import Extension
  19. from markdown.blockprocessors import BlockProcessor
  20. import xml.etree.ElementTree as etree
  21. import re
  22. class DetailsProcessor(BlockProcessor):
  23. """Details block processor."""
  24. START = re.compile(
  25. r'(?:^|\n)\?{3}(\+)? ?(?:([\w\-]+(?: +[\w\-]+)*?)?(?: +"(.*?)")|([\w\-]+(?: +[\w\-]+)*?)) *(?:\n|$)'
  26. )
  27. COMPRESS_SPACES = re.compile(r' {2,}')
  28. def test(self, parent, block):
  29. """Test block."""
  30. sibling = self.lastChild(parent)
  31. return (
  32. self.START.search(block) or
  33. (
  34. block.startswith(' ' * self.tab_length) and sibling is not None and
  35. sibling.tag.lower() == 'details'
  36. )
  37. )
  38. def run(self, parent, blocks):
  39. """Convert to details/summary block."""
  40. sibling = self.lastChild(parent)
  41. block = blocks.pop(0)
  42. m = self.START.search(block)
  43. if m:
  44. # remove the first line
  45. block = block[m.end():]
  46. # Get the details block and and the non-details content
  47. block, non_details = self.detab(block)
  48. if m:
  49. state = m.group(1)
  50. is_open = state is not None
  51. if m.group(4):
  52. class_name = self.COMPRESS_SPACES.sub(' ', m.group(4).lower())
  53. title = class_name.split(' ')[0].capitalize()
  54. else:
  55. classes = m.group(2)
  56. class_name = '' if classes is None else self.COMPRESS_SPACES.sub(' ', classes.lower())
  57. title = m.group(3)
  58. div = etree.SubElement(parent, 'details', ({'open': 'open'} if is_open else {}))
  59. if class_name:
  60. div.set('class', class_name)
  61. summary = etree.SubElement(div, 'summary')
  62. summary.text = title
  63. else:
  64. div = sibling
  65. self.parser.parseChunk(div, block)
  66. if non_details:
  67. # Insert the non-details content back into blocks
  68. blocks.insert(0, non_details)
  69. class DetailsExtension(Extension):
  70. """Add Details extension."""
  71. def extendMarkdown(self, md):
  72. """Add Details to Markdown instance."""
  73. md.registerExtension(self)
  74. md.parser.blockprocessors.register(DetailsProcessor(md.parser), "details", 105)
  75. def makeExtension(*args, **kwargs):
  76. """Return extension."""
  77. return DetailsExtension(*args, **kwargs)