Skip to content

Commit

Permalink
Merge pull request #67 from nhsengland/wd-python-script-to-list-tags
Browse files Browse the repository at this point in the history
added python script to print tags to terminal
  • Loading branch information
amaiaita authored Feb 21, 2024
2 parents 16fc773 + dd36f56 commit 4f2bccc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/our_work/template-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ summary: 'ONE LINE SUMMARY GOES HERE'
origin: 'Skunkworks (this is for legacy pages mainly, so we know where the work came from)'
tags: ['add your tags in here as a list (there is no limit)','tag 2','tag 3']
---
[comment]: <> When making your tags, it might be handy to see a list of the tags that have already been defined. Run `python utils/list-tags.py` from the terminal to print the existing tags (assuming you are in the root directory - update the path if not). You can also run it from VS Code by opening the file and clicking the "Run" button in the top right (but you'll need to have the Python extension installed to do it this way).

[comment]: <> The metadata block above is used to make the title, the tags, and the first block of "quote text" just beneath the title

[comment]: <> Make a nice visual abstract for your project, or show some key result, which makes the impact of your work clear - imagine your audience is an educated observer from the health system
Expand Down
49 changes: 49 additions & 0 deletions utils/list-tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# prints all tags appearing in the .md files in /docs to terminal
# run with: python list-tags.py
import os
import re

# Function to extract tags from a markdown file
def extract_tags_from_file(file_path):

with open(file_path, 'r') as file:

for line in file:

if not line.strip().startswith('tags:'):
continue

start_pos_of_tags = line.find('[') + 1
end_pos_of_tags = line.find(']')

tags_as_string = line[start_pos_of_tags:end_pos_of_tags]
tags_as_string = tags_as_string.replace("'", "")
tags_as_string = tags_as_string.replace('"', "")

tags_as_list = tags_as_string.split(',')

tags_as_list = [tag.strip() for tag in tags_as_list]

return tags_as_list


# Get list of markdown files
md_files = []
for root, dirs, files in os.walk('docs'):
for file in files:
if file.endswith('.md'):
md_files.append(os.path.join(root, file))

# Extract tags from each markdown file
all_tags = []
for file_path in md_files:
tags = extract_tags_from_file(file_path)
if tags:
all_tags = all_tags + tags

# Remove duplicates and sort tags
sorted_unique_tags = sorted(set(all_tags))

# Print sorted tags list
for tag in sorted_unique_tags:
print(tag)

0 comments on commit 4f2bccc

Please sign in to comment.