1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15 16"""Library for operating on Python API Guide files.""" 17 18from __future__ import absolute_import 19from __future__ import division 20from __future__ import print_function 21 22import os 23import re 24 25 26def md_files_in_dir(py_guide_src_dir): 27 """Returns a list of filename (full_path, base) pairs for guide files.""" 28 all_in_dir = [(os.path.join(py_guide_src_dir, f), f) 29 for f in os.listdir(py_guide_src_dir)] 30 return [(full, f) for full, f in all_in_dir 31 if os.path.isfile(full) and f.endswith('.md')] 32 33 34class PyGuideParser(object): 35 """Simple parsing of a guide .md file. 36 37 Descendants can override the process_*() functions (called by process()) 38 to either record information from the guide, or call replace_line() 39 to affect the return value of process(). 40 """ 41 42 def __init__(self): 43 self._lines = None 44 45 def process(self, full_path): 46 """Read and process the file at `full_path`.""" 47 with open(full_path, 'rb') as f: 48 md_string = f.read().decode('utf-8') 49 self._lines = md_string.split('\n') 50 seen = set() 51 52 in_blockquote = False 53 for i, line in enumerate(self._lines): 54 if '```' in line: 55 in_blockquote = not in_blockquote 56 57 if not in_blockquote and line.startswith('# '): 58 self.process_title(i, line[2:]) 59 elif not in_blockquote and line.startswith('## '): 60 section_title = line.strip()[3:] 61 existing_tag = re.search(' {([^}]+)} *$', line) 62 if existing_tag: 63 tag = existing_tag.group(1) 64 else: 65 tag = re.sub('[^a-zA-Z0-9]+', '_', section_title) 66 if tag in seen: 67 suffix = 0 68 while True: 69 candidate = '%s_%d' % (tag, suffix) 70 if candidate not in seen: 71 tag = candidate 72 break 73 seen.add(tag) 74 self.process_section(i, section_title, tag) 75 76 elif in_blockquote: 77 self.process_in_blockquote(i, line) 78 else: 79 self.process_line(i, line) 80 81 ret = '\n'.join(self._lines) 82 self._lines = None 83 return ret 84 85 def replace_line(self, line_number, line): 86 """Replace the contents of line numbered `line_number` with `line`.""" 87 self._lines[line_number] = line 88 89 def process_title(self, line_number, title): 90 pass 91 92 def process_section(self, line_number, section_title, tag): 93 pass 94 95 def process_in_blockquote(self, line_number, line): 96 pass 97 98 def process_line(self, line_number, line): 99 pass 100