-
Notifications
You must be signed in to change notification settings - Fork 46
/
functions.py
416 lines (354 loc) · 14.6 KB
/
functions.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import random
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.expression import func
from bs4 import BeautifulSoup
import nltk
from nltk.corpus import stopwords
from flask import request
import math
from collections import defaultdict
import json
import xml.etree.ElementTree as ET
import os
stop_words = set(stopwords.words('english'))
# Calculate the estimated reading time for the content
# Parameters:
# - content: String containing the content to calculate reading time for
# Returns:
# - Integer representing the estimated reading time in minutes
def calculate_reading_time(content):
if content is not None:
words = content.split()
reading_time = len(words) / 200 # Assume 200 words per minute
reading_time = math.ceil(reading_time) # Round up to the nearest minute
return reading_time
else:
return 0
# Convert a given text to a URL-friendly format
# Parameters:
# - text: String to be converted to a URL-friendly format
# Returns:
# - String with hyphens instead of spaces and capitalized words
def urlize(text):
text = text.strip()
text = text.replace('-', ' ').replace('_', ' ')
words = text.split()
words = [word.capitalize() if word.lower() not in stop_words else word for word in words]
text = ' '.join(words)
text = text[0].capitalize() + text[1:]
return text
# Build a data dictionary from topics and articles
# Parameters:
# - contributors: SQLAlchemy query object for contributors
# - blogs: SQLAlchemy query object for blogs
# - topics: SQLAlchemy query object for topics
# - articles: SQLAlchemy query object for articles
# Returns:
# - Dictionary containing structured data of topics and articles
def build_data_dict(contributors, blogs, topics, articles):
data_dict = {}
contributors_data = contributors.query.all()
blogs_data = blogs.query.all()
topics_data = topics.query.all()
topic_dict = defaultdict(lambda: defaultdict(list))
for topic in topics_data:
topic_dict[topic.level][topic.parent].append(topic)
def serialize_article(article):
return {
'id': article.id,
'title': article.title,
'path': article.path,
'description': article.description,
'reading_time': calculate_reading_time(article.content),
'date': article.date,
'draft': article.draft
}
def serialize_blog(blog):
return{
'id': blog.id,
'title': blog.title,
'description': blog.description,
'path': blog.path
}
def serialize_contributor(contributor):
return{
'id': contributor.id,
'name': contributor.name,
'path': contributor.path,
'description_short': contributor.description_short
}
def serialize_topic(topic, level, parent, articles):
return {
'id': topic.id,
'title': topic.title,
'path': topic.path,
'parent': topic.parent,
'level': topic.level,
'draft': topic.draft,
'childtopics': build_structure(level + 1, topic.id, articles),
'articles': [serialize_article(article) for article in articles.query.filter_by(parent=topic.id).all()]
}
def build_structure(level, parent, articles):
if level not in topic_dict:
return []
return [
serialize_topic(topic, level, parent, articles)
for topic in topic_dict[level][parent]
]
articles_examples = articles.query.filter_by(type='examples').all()
serialized_articles_examples = [serialize_article(article) for article in articles_examples]
serialized_blogs = [serialize_blog(blog) for blog in blogs_data]
serialized_contributors = [serialize_contributor(contributor) for contributor in contributors_data]
data_dict['topics'] = build_structure(1, 1, articles)
data_dict['examples'] = serialized_articles_examples
data_dict['contributors'] = serialized_contributors
data_dict['blogs'] = serialized_blogs
return data_dict
# Recursively find the full path of a topic by its parent ID
# Parameters:
# - parent_topic_id: Integer ID of the parent topic
# - Topics: SQLAlchemy model for topics
# Returns:
# - Dictionary containing the title and full path of the topic
def get_full_topic_path(parent_topic_id, Topics):
topic = Topics.query \
.with_entities(Topics.id, Topics.title, Topics.path, Topics.parent) \
.filter_by(id=parent_topic_id) \
.first()
if not topic:
return None
if not topic.parent:
return {
"title": topic.title,
"path": topic.path
}
parent_topic = get_full_topic_path(topic.parent, Topics)
if parent_topic:
full_path = f"{parent_topic['path']}/{topic.path}"
return {
"title": topic.title,
"path": full_path
}
return {
"title": topic.title,
"path": topic.path
}
# Fetch the most recently published articles
# Parameters:
# - articles: SQLAlchemy query object for articles
# - Topics: SQLAlchemy model for topics
# Returns:
# - List of dictionaries containing the title and path of recent articles
def recently_published(articles, Topics):
base_url = request.host_url.rstrip("/")
recent_articles_query = articles.query \
.with_entities(articles.title, articles.path, articles.parent) \
.order_by(func.coalesce(articles.date_modified, articles.date).desc()) \
.limit(4) \
.all()
recent_articles_dict = []
for article in recent_articles_query:
title = article.title
article_path = article.path
article_parent = article.parent
parent_topic_path = get_full_topic_path(article_parent, Topics)
if parent_topic_path:
full_path = f"{base_url}/{parent_topic_path['path']}/{article_path}"
full_dict = {"title": title, "path": full_path}
recent_articles_dict.append(full_dict)
else:
full_path = f"{base_url}/{article_path}"
recent_articles_dict.append({"title": title, "path": full_path})
return recent_articles_dict
# Generate a table of contents from HTML content
# Parameters:
# - content_html: String containing the HTML content
# Returns:
# - List of dictionaries representing the table of contents
def generate_table_of_contents(content_html):
soup = BeautifulSoup(content_html, 'html.parser')
headings = soup.find_all(['h2', 'h3'])
table_of_contents = []
current_h2 = None
for heading in headings:
level = heading.name
text = heading.text.strip()
anchor = text.lower().replace(' ', '-')
if level == 'h2':
current_h2 = {'text': text, 'anchor': anchor, 'subheadings': []}
table_of_contents.append(current_h2)
elif level == 'h3' and current_h2 is not None:
current_h2['subheadings'].append({'text': text, 'anchor': anchor})
return table_of_contents
# Generate breadcrumbs for the current page
# Parameters:
# - None
# Returns:
# - List of dictionaries representing breadcrumb items
def get_breadcrumbs():
base_url = request.host_url.rstrip("/")
current_url = request.url
path = current_url.replace(base_url, "")
url_parts = path.strip("/").split("/")
breadcrumbs = [{"name": "Home", "url": base_url}]
for i in range(1, len(url_parts)):
breadcrumb = {
"name": url_parts[i].replace("-", " ").title(),
"url": f"{base_url}/{'/'.join(url_parts[:i + 1])}/"
}
if "?utm_" in url_parts[i]:
continue
else:
breadcrumbs.append(breadcrumb)
return breadcrumbs
# Find related articles for a given article path
# Parameters:
# - article_path: String representing the path of the current article
# - articles: SQLAlchemy query object for articles
# - Topics: SQLAlchemy model for topics
# Returns:
# - List of dictionaries containing related articles' title, path, keywords, and description
def find_related_articles(article_path, articles, Topics):
current_article = articles.query.filter_by(path=article_path).first()
if not current_article:
return []
related_articles_query = articles.query \
.filter(articles.parent == current_article.parent, articles.id != current_article.id) \
.limit(3) \
.all()
if len(related_articles_query) < 1:
parent_topic = Topics.query.filter_by(id=current_article.parent).first()
if parent_topic:
sibling_categories = Topics.query \
.filter(Topics.parent == parent_topic.parent, Topics.id != current_article.parent) \
.all()
sibling_articles = []
for sibling in sibling_categories:
sibling_articles.extend(
articles.query.filter_by(parent=sibling.id).all()
)
related_articles_query.extend(random.sample(sibling_articles, min(3 - len(related_articles_query), len(sibling_articles))))
base_url = request.host_url.rstrip("/")
top_related_articles = []
for article in related_articles_query:
parent_topic_path = get_full_topic_path(article.parent, Topics)
if parent_topic_path:
full_path = f"{base_url}/{parent_topic_path['path']}/{article.path}"
else:
full_path = f"{base_url}/{article.path}"
top_related_articles.append({
"title": article.title,
"path": full_path,
"keywords": article.keywords,
"description": article.description
})
return top_related_articles
# Fetch meta data from a data object
# Parameters:
# - data_object: Object or dictionary containing meta data attributes
# Returns:
# - Dictionary containing the meta data
def fetch_meta_data(data_object):
meta_data = {}
if isinstance(data_object, dict):
title = data_object.get('title')
description = data_object.get('description')
keywords = data_object.get('keywords')
else:
title = getattr(data_object, 'title', None)
description = getattr(data_object, 'description', None)
keywords = getattr(data_object, 'keywords', None)
if title:
meta_data['title'] = title
if description:
meta_data['description'] = description
if keywords:
meta_data['keywords'] = keywords
return meta_data
# Fetch contributions for a single contributor
# Parameters:
# - Contributor: SQLAlchemy model for contributors
# - Articles: SQLAlchemy query object for articles
# - Topics: SQLAlchemy model for topics
# Returns:
# - List of dictionaries containing contributions with their full paths
def fetch_contributions_for_the_single_contributor(Contributor, Articles, Topics):
base_url = request.host_url.rstrip("/")
contributions = Articles.query.filter_by(author=Contributor.name).with_entities(Articles.id, Articles.title, Articles.path, Articles.parent).all()
contributions_with_full_path = []
for contribution in contributions:
parent_topic_path = get_full_topic_path(contribution.parent, Topics)
if parent_topic_path:
full_path = f"{base_url}/{parent_topic_path['path']}/{contribution.path}"
else:
full_path = f"{base_url}/{contribution.path}"
contributions_with_full_path.append({
"id": contribution.id,
"title": contribution.title,
"path": full_path
})
return contributions_with_full_path
def add_url_element(urlset, loc, date=None, priority="0.8", changefreq="monthly"):
url = ET.SubElement(urlset, "url")
loc_element = ET.SubElement(url, "loc")
loc_element.text = loc.lower()
priority_element = ET.SubElement(url, "priority")
priority_element.text = priority
changefreq_element = ET.SubElement(url, "changefreq")
changefreq_element.text = changefreq
if date:
lastmod_element = ET.SubElement(url, "lastmod")
lastmod_element.text = str(date)
def generate_sitemap(app, data, base_url="https://www.example.com"):
urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
def process_topics(topics, parent_path=""):
for topic in topics:
if topic['draft'] != 'true':
full_path = f"{parent_path}/{topic['path']}".strip("/")
add_url_element(urlset, f"{base_url}/topics/{full_path}/")
process_topics(topic.get('childtopics', []), full_path)
for article in topic.get('articles', []):
if article['draft'] != 'true':
article_path = f"{full_path}/{article['path']}"
article_path = article_path.lower()
add_url_element(urlset, f"{base_url}/topics/{article_path}/", date = article['date'])
# Process topics and articles
process_topics(data['topics'])
# Process examples
for example in data.get('examples', []):
if example['draft'] != 'true':
example_path = f"{base_url}/examples/{example['path']}/"
example_path = example_path.lower()
add_url_element(urlset, example_path, date=example.get('date'))
# Process blogs
for blog in data.get('blogs', []):
blog_path = f"{base_url}/blog/{blog['path']}/"
blog_path = blog_path.lower()
add_url_element(urlset, blog_path)
# Process contributors
for contributor in data.get('contributors', []):
contributor_name_path = contributor['path'].replace('-', '')
contributor_path = f"{base_url}/contributors/{contributor_name_path}/"
contributor_path = contributor_path.lower()
add_url_element(urlset, contributor_path)
# Non dynamic routes
rules = list(app.url_map.iter_rules())
if not rules:
print("No routes were registered.")
else:
for rule in app.url_map.iter_rules():
if "GET" in rule.methods and "<" not in rule.rule and len(rule.arguments) == 0:
if "/building-blocks" not in rule.rule and "/tutorials" not in rule.rule:
add_url_element(urlset, f"{base_url}{rule.rule}", priority="0.5")
tree = ET.ElementTree(urlset)
tree.write("sitemap.xml", encoding="utf-8", xml_declaration=True)
def load_popular_pages(app):
# Load pages.json
json_path = os.path.join(app.static_folder, 'json', 'pages.json')
with open(json_path, 'r') as f:
return json.load(f)
def cards_data_homepage(app):
# Load cards.json
json_path = os.path.join(app.static_folder, 'json', 'cards.json')
with open(json_path, 'r') as f:
return json.load(f)