Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd2017c3

Browse files
authored
Add Headlines of Readme to Search Entries (#142)
1 parent3d33809 commitd2017c3

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

‎components/entrytypes.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
fromabcimportABC,abstractmethod
33
fromdataclassesimportdataclass
44
fromtypingimportClassVar,List,Optional
5+
fromurllib.parseimporturljoin
56

67
fromtelegramimportInlineKeyboardMarkup
78
fromthefuzzimportfuzz
@@ -65,6 +66,49 @@ def inline_keyboard(self) -> Optional[InlineKeyboardMarkup]:
6566
returnNone
6667

6768

69+
classReadmeSection(BaseEntry):
70+
"""A section of the readme.
71+
72+
Args:
73+
name: The name of the section
74+
anchor: the URL anchor of the section
75+
"""
76+
77+
def__init__(self,name:str,anchor:str):
78+
self.name=name
79+
self.anchor=anchor
80+
81+
@property
82+
defurl(self)->str:
83+
returnurljoin(DOCS_URL,self.anchor)
84+
85+
@property
86+
defdisplay_name(self)->str:
87+
returnf"Readme{ARROW_CHARACTER}{self.name}"
88+
89+
@property
90+
defshort_name(self)->str:
91+
returnself.name
92+
93+
@property
94+
defdescription(self)->str:
95+
return"Readme of python-telegram-bot"
96+
97+
defhtml_markup(self,search_query:str=None)->str:
98+
return (
99+
f"Readme of <i>python-telegram-bot</i>\n"f"{self.html_insertion_markup(search_query)}"
100+
)
101+
102+
defhtml_insertion_markup(self,search_query:str=None)->str:
103+
returnf'<a href="{self.url}">{self.short_name}</a>'
104+
105+
defhtml_reply_markup(self,search_query:str=None)->str:
106+
returnf'<a href="{self.url}">Readme Section:{self.short_name}</a>'
107+
108+
defcompare_to_query(self,search_query:str)->float:
109+
returnfuzz.token_set_ratio(f"readme{self.name}",search_query)
110+
111+
68112
classExample(BaseEntry):
69113
"""An example in the examples directory.
70114

‎components/search.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
FAQEntry,
3434
FRDPEntry,
3535
ParamDocEntry,
36+
ReadmeSection,
3637
WikiPage,
3738
)
3839
from .githubimportGitHub
@@ -43,13 +44,14 @@ class Search:
4344
def__init__(self,github_auth:str,github_user_agent:str=USER_AGENT)->None:
4445
self.__lock=asyncio.Lock()
4546
self._docs:List[DocEntry]= []
47+
self._readme:List[ReadmeSection]= []
4648
self._official:Dict[str,str]= {}
4749
self._wiki:List[WikiPage]= []
4850
self._snippets:List[CodeSnippet]= []
4951
self._faq:List[FAQEntry]= []
5052
self._design_patterns:List[FRDPEntry]= []
5153
self.github=GitHub(auth=github_auth,user_agent=github_user_agent)
52-
self._httpx_client=httpx.AsyncClient()
54+
self._httpx_client=httpx.AsyncClient(headers=DEFAULT_HEADERS)
5355

5456
asyncdefinitialize(
5557
self,application:Application[Any,Any,Any,Any,Any,JobQueue]
@@ -76,6 +78,7 @@ async def update_job(self, context: ContextTypes.DEFAULT_TYPE) -> None:
7678
)
7779
asyncwithself.__lock:
7880
awaitasyncio.gather(
81+
context.application.create_task(self.update_readme()),
7982
context.application.create_task(self.update_docs()),
8083
context.application.create_task(self.update_wiki()),
8184
context.application.create_task(self.update_wiki_code_snippets()),
@@ -108,7 +111,7 @@ async def update_job(self, context: ContextTypes.DEFAULT_TYPE) -> None:
108111
self.multi_search_combinations.cache_clear()# pylint:disable=no-member
109112

110113
asyncdef_update_official_docs(self)->None:
111-
response=awaitself._httpx_client.get(url=OFFICIAL_URL,headers=DEFAULT_HEADERS)
114+
response=awaitself._httpx_client.get(url=OFFICIAL_URL)
112115
official_soup=BeautifulSoup(response.content,"html.parser")
113116
foranchorinofficial_soup.select("a.anchor"):
114117
if"-"notinanchor["href"]:
@@ -173,8 +176,26 @@ async def update_docs(self) -> None:
173176
)
174177
)
175178

179+
asyncdefupdate_readme(self)->None:
180+
response=awaitself._httpx_client.get(url=DOCS_URL,follow_redirects=True)
181+
readme_soup=BeautifulSoup(response.content,"html.parser")
182+
self._readme= []
183+
184+
# parse section headers from readme
185+
fortagin ["h1","h2","h3","h4","h5"]:
186+
forheadlineinreadme_soup.select(tag):
187+
# check if element is inside a hidden div - special casing for the
188+
# "Hidden Headline" we include for furo
189+
ifheadline.find_parent("div",attrs={"style":"display: none"}):
190+
continue
191+
self._readme.append(
192+
ReadmeSection(
193+
name=str(headline.contents[0]).strip(),anchor=headline.find("a")["href"]
194+
)
195+
)
196+
176197
asyncdefupdate_wiki(self)->None:
177-
response=awaitself._httpx_client.get(url=WIKI_URL,headers=DEFAULT_HEADERS)
198+
response=awaitself._httpx_client.get(url=WIKI_URL)
178199
wiki_soup=BeautifulSoup(response.content,"html.parser")
179200
self._wiki= []
180201

@@ -195,9 +216,7 @@ async def update_wiki(self) -> None:
195216
self._wiki.append(WikiPage(category="Code Resources",name="Examples",url=EXAMPLES_URL))
196217

197218
asyncdefupdate_wiki_code_snippets(self)->None:
198-
response=awaitself._httpx_client.get(
199-
url=WIKI_CODE_SNIPPETS_URL,headers=DEFAULT_HEADERS
200-
)
219+
response=awaitself._httpx_client.get(url=WIKI_CODE_SNIPPETS_URL)
201220
code_snippet_soup=BeautifulSoup(response.content,"html.parser")
202221
self._snippets= []
203222
forheadlineincode_snippet_soup.select(
@@ -211,7 +230,7 @@ async def update_wiki_code_snippets(self) -> None:
211230
)
212231

213232
asyncdefupdate_wiki_faq(self)->None:
214-
response=awaitself._httpx_client.get(url=WIKI_FAQ_URL,headers=DEFAULT_HEADERS)
233+
response=awaitself._httpx_client.get(url=WIKI_FAQ_URL)
215234
faq_soup=BeautifulSoup(response.content,"html.parser")
216235
self._faq= []
217236
forheadlineinfaq_soup.select("div#wiki-body h3"):
@@ -223,7 +242,7 @@ async def update_wiki_faq(self) -> None:
223242
)
224243

225244
asyncdefupdate_wiki_design_patterns(self)->None:
226-
response=awaitself._httpx_client.get(url=WIKI_FRDP_URL,headers=DEFAULT_HEADERS)
245+
response=awaitself._httpx_client.get(url=WIKI_FRDP_URL)
227246
frdp_soup=BeautifulSoup(response.content,"html.parser")
228247
self._design_patterns= []
229248
forheadlineinfrdp_soup.select("div#wiki-body h3,div#wiki-body h2"):
@@ -244,9 +263,10 @@ async def search(
244263
)->Optional[List[BaseEntry]]:
245264
"""Searches all available entries for appropriate results. This includes:
246265
266+
* readme sections
247267
* wiki pages
248268
* FAQ entries
249-
* Design Pattern entries entries
269+
* Design Pattern entries
250270
* Code snippets
251271
* examples
252272
* documentation
@@ -312,6 +332,7 @@ async def search(
312332
asyncwithself.__lock:
313333
ifnotsearch_entries:
314334
search_entries=itertools.chain(
335+
self._readme,
315336
self._wiki,
316337
self.github.all_examples,
317338
self._faq,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp