2626    from  collections .abc  import  Iterator 
2727
2828from  .config  import  Config 
29- from  .snippets  import  Snippet , WithTitle , Document , Section 
29+ from  .snippets  import  Snippet , WithTitle , Document , Section ,  Code 
3030from  .picker  import  pick 
3131from  .cache  import  Cache , Item 
3232from  .keyword  import  Extractor 
@@ -45,53 +45,38 @@ def extract_tags(s: Snippet) -> str:
4545        tags  +=  'd' 
4646    elif  isinstance (s , Section ):
4747        tags  +=  's' 
48+     elif  isinstance (s , Code ):
49+         tags  +=  'c' 
4850    return  tags 
4951
5052
5153def  extract_excerpt (s : Snippet ) ->  str :
5254    if  isinstance (s , Document ) and  s .title  is  not   None :
53-         return  '<'  +  s .title . text  +  '>' 
55+         return  '<'  +  s .title  +  '>' 
5456    elif  isinstance (s , Section ) and  s .title  is  not   None :
55-         return  '['  +  s .title .text  +  ']' 
57+         return  '['  +  s .title  +  ']' 
58+     elif  isinstance (s , Code ):
59+         return  '`'  +  (s .lang  +  ':' ).ljust (8 , ' ' ) +  ' '  +  s .desc  +  '`' 
5660    return  '' 
5761
5862
5963def  extract_keywords (s : Snippet ) ->  list [str ]:
6064    keywords  =  [s .docname ]
61-     # TODO: Deal with more snippet 
6265    if  isinstance (s , WithTitle ) and  s .title  is  not   None :
63-         keywords .extend (extractor .extract (s .title .text , strip_stopwords = False ))
66+         keywords .extend (extractor .extract (s .title , strip_stopwords = False ))
67+     if  isinstance (s , Code ):
68+         keywords .extend (extractor .extract (s .desc , strip_stopwords = False ))
6469    return  keywords 
6570
6671
67- def  is_document_matched (
68-     pats : dict [str , list [str ]], docname : str 
69- ) ->  dict [str , list [str ]]:
70-     """Whether the docname matched by given patterns pats""" 
71-     new_pats  =  {}
72-     for  tag , ps  in  pats .items ():
72+ def  _get_document_allowed_tags (pats : dict [str , list [str ]], docname : str ) ->  str :
73+     """Return the tags of snippets that are allowed to be picked from the document.""" 
74+     allowed_tags  =  '' 
75+     for  tags , ps  in  pats .items ():
7376        for  pat  in  ps :
7477            if  re .match (pat , docname ):
75-                 new_pats .setdefault (tag , []).append (pat )
76-     return  new_pats 
77- 
78- 
79- def  is_snippet_matched (pats : dict [str , list [str ]], s : [Snippet ], docname : str ) ->  bool :
80-     """Whether the snippet's tags and docname matched by given patterns pats""" 
81-     if  '*'  in  pats :  # Wildcard 
82-         for  pat  in  pats ['*' ]:
83-             if  re .match (pat , docname ):
84-                 return  True 
85- 
86-     not_in_pats  =  True 
87-     for  k  in  extract_tags (s ):
88-         if  k  not  in   pats :
89-             continue 
90-         not_in_pats  =  False 
91-         for  pat  in  pats [k ]:
92-             if  re .match (pat , docname ):
93-                 return  True 
94-     return  not_in_pats 
78+                 allowed_tags  +=  tags 
79+     return  allowed_tags 
9580
9681
9782def  on_config_inited (app : Sphinx , appcfg : SphinxConfig ) ->  None :
@@ -113,6 +98,7 @@ def on_env_get_outdated(
11398    removed : set [str ],
11499) ->  list [str ]:
115100    # Remove purged indexes and snippetes from db 
101+     assert  cache  is  not   None 
116102    for  docname  in  removed :
117103        del  cache [(app .config .project , docname )]
118104    return  []
@@ -126,15 +112,16 @@ def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> N
126112        )
127113        return 
128114
129-     pats  =  is_document_matched (app .config .snippet_patterns , docname )
130-     if  len ( pats )  ==   0 :
131-         logger .debug ('[snippet] skip picking because %s is not matched ' , docname )
115+     allowed_tags  =  _get_document_allowed_tags (app .config .snippet_patterns , docname )
116+     if  not   allowed_tags :
117+         logger .debug ('[snippet] skip picking: no tag allowed for document %s ' , docname )
132118        return 
133119
134120    doc  =  []
135121    snippets  =  pick (app , doctree , docname )
136122    for  s , n  in  snippets :
137-         if  not  is_snippet_matched (pats , s , docname ):
123+         # FIXME: Better filter logic. 
124+         if  extract_tags (s ) not  in   allowed_tags :
138125            continue 
139126        tpath  =  [x .astext () for  x  in  titlepath .resolve (app .env , docname , n )]
140127        if  isinstance (s , Section ):
@@ -162,6 +149,7 @@ def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> N
162149
163150
164151def  on_builder_finished (app : Sphinx , exception ) ->  None :
152+     assert  cache  is  not   None 
165153    cache .dump ()
166154
167155
0 commit comments