Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738
  1. import codecs
  2. import fnmatch
  3. import os
  4. import markdown
  5. def neighborhood(iterable, first=None, last=None):
  6. """
  7. Yield the (previous, current, next) items given an iterable.
  8. You can specify a `first` and/or `last` item for bounds.
  9. """
  10. iterator = iter(iterable)
  11. previous = first
  12. current = iterator.next() # Throws StopIteration if empty.
  13. for next in iterator:
  14. yield (previous, current, next)
  15. previous = current
  16. current = next
  17. yield (previous, current, last)
  18. def parse_markdown(file_path):
  19. """Extract title, (HTML) content and metadata from a markdown file."""
  20. parser = markdown.Markdown(extensions=["meta"])
  21. with codecs.open(file_path, "r") as source:
  22. content = parser.convert(source.read())
  23. metadata = parser.Meta if hasattr(parser, "Meta") else None
  24. title = metadata["title"][0] if metadata is not None else ""
  25. return title, content, metadata
  26. def each_markdown_from(source_dir, file_name="index.md"):
  27. """Walk across the `source_dir` and return the md file paths."""
  28. for root, dirnames, filenames in os.walk(source_dir):
  29. for filename in fnmatch.filter(filenames, file_name):
  30. yield os.path.join(root, filename)