|
| 1 | +import re |
| 2 | +from typing import Dict, List, Union |
| 3 | + |
| 4 | +_GOOGLE_SECTIONS: List[str] = [ |
| 5 | + "Args", |
| 6 | + "Returns", |
| 7 | + "Raises", |
| 8 | + "Yields", |
| 9 | + "Example", |
| 10 | + "Examples", |
| 11 | + "Attributes", |
| 12 | + "Note", |
| 13 | +] |
| 14 | + |
| 15 | +ESCAPE_RULES = { |
| 16 | + # Avoid Markdown in magic methods or filenames like __init__.py |
| 17 | + r"__(?P<text>\S+)__": r"\_\_\g<text>\_\_", |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +class Section: |
| 22 | + def __init__(self, name: str, content: str) -> None: |
| 23 | + self.name = name |
| 24 | + self.content = "" |
| 25 | + |
| 26 | + self._parse(content) |
| 27 | + |
| 28 | + def _parse(self, content: str) -> None: |
| 29 | + content = content.rstrip("\n") |
| 30 | + |
| 31 | + parts = [] |
| 32 | + cur_part = [] |
| 33 | + |
| 34 | + for line in content.split("\n"): |
| 35 | + line = line.replace(" ", "", 1) |
| 36 | + |
| 37 | + if line.startswith(" "): |
| 38 | + # Continuation from a multiline description |
| 39 | + cur_part.append(line) |
| 40 | + continue |
| 41 | + |
| 42 | + if cur_part: |
| 43 | + # Leaving multiline description |
| 44 | + parts.append(cur_part) |
| 45 | + cur_part = [line] |
| 46 | + else: |
| 47 | + # Entering new description part |
| 48 | + cur_part.append(line) |
| 49 | + |
| 50 | + # Last part |
| 51 | + parts.append(cur_part) |
| 52 | + |
| 53 | + # Format section |
| 54 | + for part in parts: |
| 55 | + self.content += "- {}\n".format(part[0]) |
| 56 | + |
| 57 | + for line in part[1:]: |
| 58 | + self.content += " {}\n".format(line) |
| 59 | + |
| 60 | + self.content = self.content.rstrip("\n") |
| 61 | + |
| 62 | + def as_markdown(self) -> str: |
| 63 | + return "# {}\n\n{}\n\n".format(self.name, self.content) |
| 64 | + |
| 65 | + def __repr__(self) -> str: |
| 66 | + return "Section(name={}, content={})".format(self.name, self.content) |
| 67 | + |
| 68 | + |
| 69 | +class GoogleDocstring: |
| 70 | + def __init__(self, docstring: str) -> None: |
| 71 | + self.sections: list[Section] = [] |
| 72 | + self.description: str = "" |
| 73 | + |
| 74 | + self._parse(docstring) |
| 75 | + |
| 76 | + def _parse(self, docstring: str) -> None: |
| 77 | + self.sections = [] |
| 78 | + self.description = "" |
| 79 | + |
| 80 | + buf = "" |
| 81 | + cur_section = "" |
| 82 | + |
| 83 | + for line in docstring.split("\n"): |
| 84 | + if is_section(line): |
| 85 | + # Entering new section |
| 86 | + if cur_section: |
| 87 | + # Leaving previous section, save it and reset buffer |
| 88 | + self.sections.append(Section(cur_section, buf)) |
| 89 | + buf = "" |
| 90 | + |
| 91 | + # Remember currently parsed section |
| 92 | + cur_section = line.rstrip(":") |
| 93 | + continue |
| 94 | + |
| 95 | + # Parse section content |
| 96 | + if cur_section: |
| 97 | + buf += line + "\n" |
| 98 | + else: |
| 99 | + # Before setting cur_section, we're parsing the function description |
| 100 | + self.description += line + "\n" |
| 101 | + |
| 102 | + # Last section |
| 103 | + self.sections.append(Section(cur_section, buf)) |
| 104 | + |
| 105 | + def as_markdown(self) -> str: |
| 106 | + text = self.description |
| 107 | + |
| 108 | + for section in self.sections: |
| 109 | + text += section.as_markdown() |
| 110 | + |
| 111 | + return text.rstrip("\n") + "\n" # Only keep one last newline |
| 112 | + |
| 113 | + |
| 114 | +def is_section(line: str) -> bool: |
| 115 | + for section in _GOOGLE_SECTIONS: |
| 116 | + if re.search(r"{}:".format(section), line): |
| 117 | + return True |
| 118 | + |
| 119 | + return False |
| 120 | + |
| 121 | + |
| 122 | +def looks_like_google(value: str) -> bool: |
| 123 | + for section in _GOOGLE_SECTIONS: |
| 124 | + if re.search(r"{}:\n".format(section), value): |
| 125 | + return True |
| 126 | + |
| 127 | + return False |
| 128 | + |
| 129 | + |
| 130 | +def google_to_markdown(text: str, extract_signature: bool = True) -> str: |
| 131 | + # Escape parts we don't want to render |
| 132 | + for pattern, replacement in ESCAPE_RULES.items(): |
| 133 | + text = re.sub(pattern, replacement, text) |
| 134 | + |
| 135 | + docstring = GoogleDocstring(text) |
| 136 | + |
| 137 | + return docstring.as_markdown() |
0 commit comments