| @@ -11,6 +11,7 @@ from flask_login import login_required, current_user | |||
| from .models import Resource | |||
| from .resources import * | |||
| from .relationships import * | |||
| from .practice_markdown import * | |||
| from . import db | |||
| import os | |||
| import re | |||
| @@ -0,0 +1,53 @@ | |||
| # @name: practice_markdown.py | |||
| # @creation_date: 2024-02-07 | |||
| # @license: The MIT License <https://opensource.org/licenses/MIT> | |||
| # @author: Simon Bowie <ad7588@coventry.ac.uk> | |||
| # @purpose: functions for retrieving Markdown for practices | |||
| # @acknowledgements: | |||
| import os | |||
| import markdown | |||
| # function to get practice from Markdown file | |||
| def get_practice_markdown(practice_name, option='html'): | |||
| practice_name = practice_name.replace(" ", "_") | |||
| file_path = f'content/practices/{practice_name}.md' | |||
| if not os.path.exists(file_path): | |||
| return "" | |||
| try: | |||
| with open(file_path, 'r') as f: | |||
| practice_text = f.read() | |||
| if option == 'html': | |||
| practice_text = markdown.markdown(practice_text) | |||
| return practice_text | |||
| except Exception as e: | |||
| return f"Error: {str(e)}" | |||
| # function to write new or edited practice to Markdown file | |||
| def write_practice_markdown(practice_name, markdown): | |||
| practice_name = practice_name.replace(" ", "_") | |||
| with open(f'content/practices/{practice_name}.md', 'w+') as f: | |||
| f.write(markdown) | |||
| # function to extract only the first paragraph of practice Markdown | |||
| def extract_first_paragraph(markdown): | |||
| # Split the text into lines | |||
| lines = markdown.split("\n") | |||
| # Initialize a flag to track when we find the first paragraph | |||
| paragraph = [] | |||
| for line in lines: | |||
| # Ignore headings (lines starting with #) | |||
| if line.startswith("#"): | |||
| continue | |||
| # If the line is not empty, it's part of a paragraph | |||
| if line.strip(): | |||
| paragraph.append(line.strip()) | |||
| elif paragraph: # Stop once we have collected a paragraph and hit an empty line | |||
| break | |||
| return " ".join(paragraph) | |||
| @@ -8,6 +8,7 @@ | |||
| from .models import Resource | |||
| from .models import Relationship | |||
| from . import db | |||
| from .practice_markdown import get_practice_markdown, extract_first_paragraph | |||
| import markdown | |||
| # function to retrieve linked resources | |||
| @@ -46,8 +47,13 @@ def append_relationships(resource): | |||
| else: | |||
| resource.__dict__['tools'].append(relationship) | |||
| elif relationship.type == 'practice': | |||
| # render Markdown as HTML | |||
| relationship.description = markdown.markdown(relationship.description) | |||
| if not relationship.description: | |||
| # fill the description field with the first paragraph of the associated practice Markdown file | |||
| practice_markdown = get_practice_markdown(relationship.name, 'markdown') | |||
| if practice_markdown: | |||
| description = extract_first_paragraph(practice_markdown) | |||
| relationship.description = description | |||
| if 'practices' not in resource.__dict__.keys(): | |||
| resource.__dict__['practices'] = [] | |||
| resource.__dict__['practices'].append(relationship) | |||
| @@ -44,42 +44,6 @@ def get_full_resource(resource_id): | |||
| resource.__dict__['commitDate'] = date | |||
| return resource | |||
| # function to get practice from Markdown file | |||
| def get_practice_markdown(practice_name, option='html'): | |||
| file_name = practice_name.replace(" ", "_") | |||
| with open(f'content/practices/{file_name}.md', 'r') as f: | |||
| practice_text = f.read() | |||
| if option == 'html': | |||
| practice_text = markdown.markdown(practice_text) | |||
| return practice_text | |||
| # function to write new or edited practice to Markdown file | |||
| def write_practice_markdown(practice_name, markdown): | |||
| practice_name = practice_name.replace(" ", "_") | |||
| with open(f'content/practices/{practice_name}.md', 'w+') as f: | |||
| f.write(markdown) | |||
| # function to extract only the first paragraph of practice Markdown | |||
| def extract_first_paragraph(markdown): | |||
| # Split the text into lines | |||
| lines = markdown.split("\n") | |||
| # Initialize a flag to track when we find the first paragraph | |||
| paragraph = [] | |||
| for line in lines: | |||
| # Ignore headings (lines starting with #) | |||
| if line.startswith("#"): | |||
| continue | |||
| # If the line is not empty, it's part of a paragraph | |||
| if line.strip(): | |||
| paragraph.append(line.strip()) | |||
| elif paragraph: # Stop once we have collected a paragraph and hit an empty line | |||
| break | |||
| return " ".join(paragraph) | |||
| # function to retrieve data about a curated list of resources | |||
| def get_curated_resources(resource_ids): | |||
| resources = Resource.query.filter(Resource.id.in_(resource_ids)).filter_by(published=True).order_by(func.random()).all() | |||
| @@ -129,47 +129,45 @@ | |||
| {% endmacro %} | |||
| {% macro relationships_links(resource) %} | |||
| {% if resource.tools %} | |||
| <div class=""> | |||
| <div class="px-4 mt-16 lg:mt-0"> | |||
| Tools | |||
| </div> | |||
| {% for tool in resource.tools %} | |||
| <div class="cell"> | |||
| {{ resource_lead(tool,size=2) }} | |||
| </div> | |||
| {% endfor %} | |||
| {% if resource.tools %} | |||
| <div class=""> | |||
| <div class="px-4 mt-16 lg:mt-0"> | |||
| Tools | |||
| </div> | |||
| {% for tool in resource.tools %} | |||
| <div class="cell"> | |||
| {{ resource_lead(tool,size=2) }} | |||
| </div> | |||
| {% endfor %} | |||
| </div> | |||
| {% endif %} | |||
| {% if resource.books %} | |||
| <div class=""> | |||
| <div class="px-4 mt-16 lg:mt-0"> | |||
| Books | |||
| </div> | |||
| {% endif %} | |||
| {% if resource.books %} | |||
| <div class=""> | |||
| <div class="px-4 mt-16 lg:mt-0"> | |||
| Books | |||
| </div> | |||
| {% for book in resource.books %} | |||
| <div class="cell"> | |||
| {{ resource_lead(book,size=2) }} | |||
| </div> | |||
| {% endfor %} | |||
| </div> | |||
| {% endif %} | |||
| {% if resource.practices %} | |||
| <div class=""> | |||
| <div class="px-4 mt-16 lg:mt-0"> | |||
| Practices | |||
| {% for book in resource.books %} | |||
| <div class="cell"> | |||
| {{ resource_lead(book,size=2) }} | |||
| </div> | |||
| {% endfor %} | |||
| </div> | |||
| {% endif %} | |||
| {% if resource.practices %} | |||
| <div class=""> | |||
| <div class="px-4 mt-16 lg:mt-0"> | |||
| Practices | |||
| </div> | |||
| {% for practice in resource.practices %} | |||
| <div class="cell"> | |||
| {{ resource_lead(practice,size=2) }} | |||
| {% for practice in resource.practices %} | |||
| <div class="cell"> | |||
| {{ resource_lead(practice,size=2) }} | |||
| </div> | |||
| {% endfor %} | |||
| </div> | |||
| {% endfor %} | |||
| </div> | |||
| {% endif %} | |||
| {% endif %} | |||
| {% endmacro %} | |||
| {% macro resource_list(resource, loop, show_number=true) %} | |||