Format inline code in .md file
(path: str)
| 275 | |
| 276 | |
| 277 | def format_inline_code(path: str): |
| 278 | """Format inline code in .md file""" |
| 279 | if not path.endswith(".md"): |
| 280 | return |
| 281 | with open(path, "r", encoding="utf-8") as f: |
| 282 | content = f.read() |
| 283 | root = path[: path.rfind("/")] |
| 284 | for suf in code_blocks: |
| 285 | res = re.findall(f"```{suf}\n(.*?)```", content, re.S) |
| 286 | for block in res or []: |
| 287 | # skip empty code block |
| 288 | if not block or not block.strip(): |
| 289 | continue |
| 290 | if suf in ["c", "cpp", "java", "go"]: |
| 291 | file = f"{root}/tmp.{suf}" |
| 292 | with open(file, "w", encoding="utf-8") as f: |
| 293 | f.write(block) |
| 294 | if suf == "go": |
| 295 | add_header(file) |
| 296 | os.system(f'gofmt -w "{file}"') |
| 297 | remove_header(file) |
| 298 | else: |
| 299 | os.system(f'npx clang-format -i --style=file "{file}"') |
| 300 | with open(file, "r", encoding="utf-8") as f: |
| 301 | new_block = f.read() |
| 302 | if not new_block.endswith("\n"): |
| 303 | new_block += "\n" |
| 304 | content = content.replace(block, new_block) |
| 305 | os.remove(file) |
| 306 | elif suf == "python": |
| 307 | new_block = black.format_str( |
| 308 | block, mode=black.FileMode(string_normalization=False) |
| 309 | ) |
| 310 | content = content.replace(block, new_block) |
| 311 | elif suf == "sql": |
| 312 | for func in functions_to_replace: |
| 313 | pattern = r"\b{}\s*\(".format(func) |
| 314 | new_block = re.sub( |
| 315 | pattern, f"{func.upper()}(", block, flags=re.IGNORECASE |
| 316 | ) |
| 317 | content = content.replace(block, new_block) |
| 318 | block = new_block |
| 319 | elif suf == "rust": |
| 320 | file = f"{root}/tmp.rs" |
| 321 | with open(file, "w", encoding="utf-8") as f: |
| 322 | f.write(block) |
| 323 | os.system(f'rustfmt "{file}"') |
| 324 | with open(file, "r", encoding="utf-8") as f: |
| 325 | new_block = f.read() |
| 326 | if not new_block.endswith("\n"): |
| 327 | new_block += "\n" |
| 328 | content = content.replace(block, new_block) |
| 329 | os.remove(file) |
| 330 | |
| 331 | with open(path, "w", encoding="utf-8") as f: |
| 332 | f.write(content) |
| 333 | |
| 334 |
no test coverage detected