11import logging
22import os
3- import re
43from .structured_data import StructuredData
5- from .syntax import Command
6- from .util import die , command_filename
4+ from .util import die
75
86
97class Markdown :
@@ -23,19 +21,22 @@ class Markdown:
2321 }
2422
2523 def __init__ (self , filepath : str , warnings : bool = False ):
24+ logging .debug ("ENTERING: " )
2625 self .filepath = filepath
2726 self .warnings = warnings
2827 self .fm_data = dict ()
2928 self .fm_type = self .FM_TYPES .get ('---\n ' )
3029 self .fm_ext = self .fm_type .get ('ext' )
3130 self .payload = ''
3231 if not self .filepath or not os .path .exists (self .filepath ):
32+ logging .debug ("EXITING: " )
3333 return
3434 with open (self .filepath , 'r' ) as f :
3535 payload = f .readlines ()
3636 if len (payload ) == 0 :
3737 self .fm_type = self .FM_TYPES .get ('---\n ' )
3838 self .fm_ext = self .fm_type .get ('ext' )
39+ logging .debug ("EXITING: " )
3940 return
4041 i = 0
4142 while i < len (payload ):
@@ -51,6 +52,7 @@ def __init__(self, filepath: str, warnings: bool = False):
5152 self .payload = '' .join (payload )
5253 self .fm_type = self .FM_TYPES .get ('---\n ' )
5354 self .fm_ext = self .fm_type .get ('ext' )
55+ logging .debug ("EXITING: " )
5456 return
5557 eof , self .fm_ext = self .fm_type .get ('eof' ), self .fm_type .get ('ext' )
5658 found = False
@@ -68,28 +70,10 @@ def __init__(self, filepath: str, warnings: bool = False):
6870 self .fm_data .update (StructuredData .loads (
6971 self .fm_ext , '' .join (payload [i + 1 :j ])))
7072 self .payload = '' .join (payload [j + 1 :])
71-
72- def add_github_metadata (self , github_repo : str , github_branch : str , github_path : str ) -> None :
73- if self .fm_data .get ('github_repo' ):
74- return
75- self .fm_data ['github_repo' ] = github_repo
76- self .fm_data ['github_branch' ] = github_branch
77- self .fm_data ['github_path' ] = github_path
78-
79- def report_links (self ) -> None :
80- links = re .findall (r'(\[.+\])(\(.+\))' , self .payload )
81- exc = ['./' , '#' , '/commands' , '/community' , '/docs' , '/topics' ]
82- for link in links :
83- ex = False
84- for e in exc :
85- if link [1 ].startswith (f'({ e } ' ):
86- ex = True
87- break
88- if not ex :
89- print (f'"{ link [1 ]} ","{ link [0 ]} ","{ self .filepath } "' )
73+ logging .debug ("EXITING: " )
9074
9175 def persist (self ) -> None :
92- # self.report_links( )
76+ logging . debug ( "ENTERING: " )
9377 payload = self .payload
9478 if self .fm_type :
9579 fm = StructuredData .dumps (self .fm_ext , self .fm_data )
@@ -105,146 +89,4 @@ def persist(self) -> None:
10589
10690 with open (self .filepath , 'w' ) as f :
10791 f .write (payload )
108-
109- @staticmethod
110- def get_command_tokens (arguments : dict ) -> set :
111- """ Extract tokens from command arguments """
112- rep = set ()
113- if type (arguments ) is list :
114- for arg in arguments :
115- rep = rep .union (Markdown .get_command_tokens (arg ))
116- else :
117- if 'token' in arguments :
118- rep .add (arguments ['token' ])
119- if 'arguments' in arguments :
120- for arg in arguments ['arguments' ]:
121- rep = rep .union (Markdown .get_command_tokens (arg ))
122- return rep
123-
124- @staticmethod
125- def make_command_linkifier (commands : dict , name : str ):
126- """
127- Returns a function (for re.sub) that converts valid ticked command names to
128- markdown links. This excludes the command in the context, as well as any of
129- its arguments' tokens.
130- """
131- if name :
132- exclude = set ([name ])
133- tokens = Markdown .get_command_tokens (commands .get (name ))
134- exclude .union (tokens )
135- else :
136- exclude = set ()
137-
138- def linkifier (m ):
139- command = m .group (1 )
140- if command in commands and command not in exclude :
141- return f'[`{ command } `]({{{{< relref "/commands/{ command_filename (command )} " >}}}})'
142- else :
143- return m .group (0 )
144- return linkifier
145-
146- def generate_commands_links (self , name : str , commands : dict , payload : str ) -> str :
147- """ Generate markdown links for back-ticked commands """
148- linkifier = Markdown .make_command_linkifier (commands , name )
149- rep = re .sub (r'`([A-Z][A-Z-_ \.]*)`' , linkifier , payload )
150- rep = re .sub (r'`!([A-Z][A-Z-_ \.]*)`' , lambda x : f'`{ x [1 ]} `' , rep )
151- return rep
152-
153- @staticmethod
154- def get_cli_shortcode (m ):
155- snippet = m [1 ]
156- start = f'{{{{% redis-cli %}}}}'
157- end = f'{{{{% /redis-cli %}}}}'
158- return f'{ start } \n { snippet .strip ()} \n { end } \n '
159-
160- @staticmethod
161- def convert_cli_snippets (payload ):
162- """ Convert the ```cli notation to Hugo shortcode syntax """
163- rep = re .sub (r'```cli(.*?)```' ,
164- Markdown .get_cli_shortcode , payload , flags = re .S )
165- return rep
166-
167- @staticmethod
168- def convert_reply_shortcuts (payload ):
169- """ Convert RESP reply type shortcuts to links """
170- def reply (x ):
171- resp = {
172- 'simple-string' : ('simple-strings' , 'Simple string reply' ),
173- 'simple-error' : ('simple-errors' , 'Simple error reply' ),
174- 'integer' : ('integers' , 'Integer reply' ),
175- 'bulk-string' : ('bulk-strings' , 'Bulk string reply' ),
176- 'array' : ('arrays' , 'Array reply' ),
177- 'nil' : ('bulk-strings' , 'Nil reply' ),
178- 'null' : ('nulls' , 'Null reply' ),
179- 'boolean' : ('booleans' , 'Boolean reply' ),
180- 'double' : ('doubles' , 'Double reply' ),
181- 'big-number' : ('big-numbers' , 'Big number reply' ),
182- 'bulk-error' : ('bulk-errors' , 'Bulk error reply' ),
183- 'verbatim-string' : ('verbatim-strings' , 'Verbatim string reply' ),
184- 'map' : ('maps' , 'Map reply' ),
185- 'set' : ('sets' , 'Set reply' ),
186- 'push' : ('pushes' , 'Push reply' )
187- }
188- rep = resp .get (x .group (1 ), None )
189- if rep :
190- return f'[{ rep [1 ]} ](/docs/reference/protocol-spec#{ rep [0 ]} )'
191- return f'[]'
192-
193- rep = re .sub (r'@([a-z\-]+)-reply' , reply , payload )
194- return rep
195-
196- @staticmethod
197- def convert_command_sections (payload ):
198- """ Converts redis-doc section headers to MD """
199- rep = re .sub (r'@examples\n' ,
200- '## Examples\n ' , payload )
201- rep = re .sub (r'@return\n' ,
202- '## Return\n ' , rep )
203- return rep
204-
205- def add_command_frontmatter (self , name , commands ):
206- """ Sets a JSON FrontMatter payload for a command page """
207- data = commands .get (name )
208- c = Command (name , data )
209- data .update ({
210- 'title' : name ,
211- 'linkTitle' : name ,
212- 'description' : data .get ('summary' ),
213- 'syntax_str' : str (c ),
214- 'syntax_fmt' : c .syntax (),
215- 'hidden' : c .isPureContainer () or c .isHelpCommand ()
216- })
217- if 'replaced_by' in data :
218- data ['replaced_by' ] = self .generate_commands_links (
219- name , commands , data .get ('replaced_by' ))
220- self .fm_type = self .FM_TYPES .get ('---\n ' )
221- self .fm_ext = self .fm_type .get ('ext' )
222- self .fm_data .update (data )
223-
224- def process_command (self , name , commands ):
225- """ New command processing logic """
226- logging .debug (f'Processing command { self .filepath } ' )
227- self .payload = self .generate_commands_links (
228- name , commands , self .payload )
229- self .payload = self .convert_command_sections (self .payload )
230- self .payload = self .convert_reply_shortcuts (self .payload )
231- self .payload = self .convert_cli_snippets (self .payload )
232- self .add_command_frontmatter (name , commands )
233- self .persist ()
234-
235- def process_doc (self , commands ):
236- """ New doc processing logic """
237- logging .debug (f'Processing document { self .filepath } ' )
238- self .payload = self .generate_commands_links (
239- None , commands , self .payload )
240- self .persist ()
241-
242- def patch_module_paths (self , module_id : str , module_path ) -> None :
243- """ Replaces absolute module documentation links """
244- def rep (x ):
245- if x .group (2 ).startswith (f'(/{ module_id } /' ):
246- r = f'{ x .group (1 )} (/{ module_path } /{ x .group (2 )[len (module_id )+ 3 :- 1 ]} )'
247- return r
248- else :
249- return x .group (0 )
250- self .payload = re .sub (f'(\[.+?\])(\(.+?\))' , rep , self .payload )
92+ logging .debug ("EXITING: " )
0 commit comments