Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

53 lines
1.7KB

  1. # @name: practice_markdown.py
  2. # @creation_date: 2024-02-07
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: functions for retrieving Markdown for practices
  6. # @acknowledgements:
  7. import os
  8. import markdown
  9. # function to get practice from Markdown file
  10. def get_practice_markdown(practice_name, option='html'):
  11. practice_name = practice_name.replace(" ", "_")
  12. file_path = f'content/practices/{practice_name}.md'
  13. if not os.path.exists(file_path):
  14. return ""
  15. try:
  16. with open(file_path, 'r') as f:
  17. practice_text = f.read()
  18. if option == 'html':
  19. practice_text = markdown.markdown(practice_text)
  20. return practice_text
  21. except Exception as e:
  22. return f"Error: {str(e)}"
  23. # function to write new or edited practice to Markdown file
  24. def write_practice_markdown(practice_name, markdown):
  25. practice_name = practice_name.replace(" ", "_")
  26. with open(f'content/practices/{practice_name}.md', 'w+') as f:
  27. f.write(markdown)
  28. # function to extract only the first paragraph of practice Markdown
  29. def extract_first_paragraph(markdown):
  30. # Split the text into lines
  31. lines = markdown.split("\n")
  32. # Initialize a flag to track when we find the first paragraph
  33. paragraph = []
  34. for line in lines:
  35. # Ignore headings (lines starting with #)
  36. if line.startswith("#"):
  37. continue
  38. # If the line is not empty, it's part of a paragraph
  39. if line.strip():
  40. paragraph.append(line.strip())
  41. elif paragraph: # Stop once we have collected a paragraph and hit an empty line
  42. break
  43. return " ".join(paragraph)