-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtag.py
95 lines (65 loc) · 2.43 KB
/
tag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import click
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from app.term.models import Term, Tag
app = create_app()
app.app_context().push()
db = SQLAlchemy(app)
# regular expression for finding tags
import re
ref_regex = re.compile("#\{\s*(([gstkm])\s*:+)?\s*([^}|]*?)(\s*\|+\s*([^}]*?))?\s*\}")
ixuniq = "xq"
ixqlen = len(ixuniq)
tagstart = "#{" # final space is important
# find all the term definitions that contain the old style tags
def g_tagged_terms():
g_terms = Term.query.filter(Term.definition.contains("#{g"), Term.status == "published")
for term in g_terms:
print(term.term_string)
print (term.definition)
print("-----------")
print(g_terms.count())
def list_tags_in_definition():
# get all terms with the tag #{t
linked_terms = Term.query.filter(Term.definition.contains("#{"), Term.status == "published")
start, end = 0, 0
# iterate through all the defintions with each loop extracting the tags
for term in linked_terms:
definition = term.definition
# find all the tags in the definition
term_tags = ref_regex.findall(definition)
# print the term and the tags
print("term string: " + term.term_string)
# list all the tags in the definition
for tag in term_tags:
# isolate the id of the term that is a tag
term_id = tag[3][3:]
# isolate the tag value
tag_string = tag[2]
# build the tag string to replace (the old style tag)
tag_to_replace = tagstart + tag[0] + " " + tag[2] + tag[3] + "}"
# build the replacement URL
replacement_url = '['+ tag_string + ']' + '(https://n2t.net/99152/' + term_id + ')'
if tag_to_replace.startswith("#{t: "):
#print("replace " + tag_to_replace + " with " + replacement_url)
definition = definition.replace(tag_to_replace, replacement_url)
print()
print(definition)
term.definition = definition
term.save()
print("total found: {}".format(linked_terms.count()))
# add the commands to the cli
@click.group()
def tag():
pass
# register each command to respond to python tag.py command
@click.command()
def listtags():
list_tags_in_definition()
@click.command()
def gtags():
g_tagged_terms()
tag.add_command(listtags)
tag.add_command(gtags)
if __name__ == "__main__":
tag()