📜  python markdown indent - Python (1)

📅  最后修改于: 2023-12-03 15:18:56.485000             🧑  作者: Mango

Python Markdown Indent

Python Markdown Indent is a Python module that provides a simple way to add code indentation to your markdown files. This module uses Python's built-in textwrap module to apply indentation to your code blocks.

Installation

You can install Python Markdown Indent using pip:

pip install markdown_indent
Usage

To use Python Markdown Indent, simply import the module and use the indent function to add indentation to your code blocks:

import markdown_indent

code_block = """
def greet(name):
print(f"Hello, {name}!")

greet("World")
"""

indented_code = markdown_indent.indent(code_block, "    ") #four spaces

print(indented_code)

The indent function takes two arguments: the code block to be indented and the string to be used as the indentation. By default, the indentation string is two spaces.

Example
import markdown_indent

code_block = """
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

for i in range(10):
print(fibonacci(i))
"""

indented_code = markdown_indent.indent(code_block, "    ") #four spaces

print(indented_code)

Output:

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

for i in range(10):
    print(fibonacci(i))
Conclusion

Python Markdown Indent is a simple and easy-to-use module that can help you add code indentation to your markdown files. Whether you're writing technical documentation or creating a blog post, this module can help make your code snippets more readable and easier to understand.