MCPcopy
hub / github.com/pandas-dev/pandas / blog_add_posts

Method blog_add_posts

web/pandas_web.py:94–158  ·  view source on GitHub ↗

Given the blog feed defined in the configuration yaml, this context preprocessor fetches the posts in the feeds, and returns the relevant information for them (sorted from newest to oldest).

(context)

Source from the content-addressed store, hash-verified

92
93 @staticmethod
94 def blog_add_posts(context):
95 """
96 Given the blog feed defined in the configuration yaml, this context
97 preprocessor fetches the posts in the feeds, and returns the relevant
98 information for them (sorted from newest to oldest).
99 """
100 tag_expr = re.compile("<.*?>")
101 posts = []
102 # posts from the file system
103 if context["blog"]["posts_path"]:
104 posts_path = context["source_path"] / context["blog"]["posts_path"]
105 for fname in posts_path.iterdir():
106 if fname.name.startswith("index."):
107 continue
108 link = f"/{context['blog']['posts_path']}/{fname.stem}.html"
109 md = markdown.Markdown(
110 extensions=context["main"]["markdown_extensions"]
111 )
112 with fname.open(encoding="utf-8") as f:
113 html = md.convert(f.read())
114 title = md.Meta["title"][0]
115 summary = re.sub(tag_expr, "", html)
116 try:
117 body_position = summary.index(title) + len(title)
118 except ValueError as err:
119 raise ValueError(
120 f'Blog post "{fname}" should have a markdown header '
121 f'corresponding to its "Title" element "{title}"'
122 ) from err
123 summary = " ".join(summary[body_position:].split(" ")[:30])
124 posts.append(
125 {
126 "title": title,
127 "author": context["blog"]["author"],
128 "published": datetime.datetime.strptime(
129 md.Meta["date"][0], "%Y-%m-%d"
130 ),
131 "feed": context["blog"]["feed_name"],
132 "link": link,
133 "description": summary,
134 "summary": summary,
135 }
136 )
137 # posts from rss feeds
138 for feed_url in context["blog"]["feed"]:
139 feed_data = feedparser.parse(feed_url)
140 for entry in feed_data.entries:
141 published = datetime.datetime.fromtimestamp(
142 time.mktime(entry.published_parsed)
143 )
144 summary = re.sub(tag_expr, "", entry.summary)
145 posts.append(
146 {
147 "title": entry.title,
148 "author": entry.author,
149 "published": published,
150 "feed": feed_data["feed"]["title"],
151 "link": entry.link,

Callers

nothing calls this directly

Calls 10

startswithMethod · 0.80
splitMethod · 0.80
openMethod · 0.45
convertMethod · 0.45
readMethod · 0.45
subMethod · 0.45
indexMethod · 0.45
joinMethod · 0.45
appendMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected