Skip to content

Commit

Permalink
v0.0.4 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
mantreshkhurana committed Feb 7, 2025
1 parent 0dd2484 commit 545a4f6
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 317 deletions.
185 changes: 30 additions & 155 deletions example.md
Original file line number Diff line number Diff line change
@@ -1,164 +1,39 @@
# Markdown Worker Examples
# Example Markdown File

The `MarkdownParser` class provides various methods to parse and manipulate Markdown files. Here's a detailed explanation of each function and its usage:
## Introduction
This is a simple markdown file used for testing the MarkdownParser class.

## Class: MarkdownParser
## Features
- Reads Markdown files
- Extracts headers and paragraphs
- Searches for specific headings
- Converts Markdown to HTML
- Fetches Markdown from URLs
- Supports advanced formatting (blockquotes, horizontal rules, tables, and images)
- Provides interactive CLI mode

### `__init__(self, filename=None)`
## Code Example
```python
print("Hello, World!")
```

- Description: Constructor for the `MarkdownParser` class.
- Parameters:
- `filename` (optional): A string representing the filename of the Markdown file to read.
- Usage:
## Blockquote Example
> This is a blockquote example.
```python
parser = MarkdownParser('example.md')
```
## Horizontal Rule Example
---

### `read_markdown_file(self)`
## Table Example
| Header 1 | Header 2 |
|----------|----------|
| Data 1 | Data 2 |
| Data 3 | Data 4 |

- Description: Reads the content of the Markdown file and stores it in the `markdown_text` attribute.
- Returns: The content of the Markdown file as a string.
- Usage:
## Image Example
![Alt Text](https://example.com/image.jpg)

```python
parser = MarkdownParser('example.md')
content = parser.read_markdown_file()
```
## Link Example
[Mantresh](https://www.mantreshkhurana.com)

### `extract_headers_and_paragraphs(self)`

- Description: Extracts headers and paragraphs from the Markdown file.
- Returns:
- `headers`: A list of strings containing the header titles.
- `paragraphs`: A list of strings containing the content of paragraphs.
- `header_indices`: A list of integers representing the line numbers of the headers in the Markdown file.
- Usage:

```python
parser = MarkdownParser('example.md')
headers, paragraphs, header_indices = parser.extract_headers_and_paragraphs()
```

### `search_heading(self, heading_to_search)`

- Description: Searches for a specific header in the Markdown file and returns its content.
- Parameters:
- `heading_to_search`: A string representing the header title to search for.
- Returns: The content of the header as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
heading_content = parser.search_heading('Introduction')
```

### `read_complete_file(self)`

- Description: Returns the complete content of the Markdown file.
- Returns: The content of the entire Markdown file as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
complete_content = parser.read_complete_file()
```

### `read_line(self, line_number)`

- Description: Reads a specific line from the Markdown file.
- Parameters:
- `line_number`: An integer representing the line number to read (0-based index).
- Returns: The content of the specified line as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
line_content = parser.read_line(5)
```

### `read_word(self, line_number, word_number)`

- Description: Reads a specific word from a given line in the Markdown file.
- Parameters:
- `line_number`: An integer representing the line number (0-based index) to read.
- `word_number`: An integer representing the word number (0-based index) to read.
- Returns: The specified word from the specified line as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
word_content = parser.read_word(10, 3)
```

### `read_character(self, line_number, word_number, character_number)`

- Description: Reads a specific character from a given word in a given line in the Markdown file.
- Parameters:
- `line_number`: An integer representing the line number (0-based index) to read.
- `word_number`: An integer representing the word number (0-based index) to read.
- `character_number`: An integer representing the character number (0-based index) to read.
- Returns: The specified character from the specified word in the specified line as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
character = parser.read_character(15, 2, 4)
```

### `read_character_from_line(self, line_number, character_number)`

- Description: Reads a specific character from a given line in the Markdown file.
- Parameters:
- `line_number`: An integer representing the line number (0-based index) to read.
- `character_number`: An integer representing the character number (0-based index) to read.
- Returns: The specified character from the specified line as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
character = parser.read_character_from_line(20, 10)
```

### `read_character_from_file(self, character_number)`

- Description: Reads a specific character from the entire Markdown file.
- Parameters:
- `character_number`: An integer representing the character number (0-based index) to read.
- Returns: The specified character from the Markdown file as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
character = parser.read_character_from_file(50)
```

### `read_word_from_line(self, line_number, word_number)`

- Description: Reads a specific word from a given line in the Markdown file.
- Parameters:
- `line_number`: An integer representing the line number (0-based index) to read.
- `word_number`: An integer representing the word number (0-based index) to read.
- Returns: The specified word from the specified line as a string.
- Usage:

```python
parser = MarkdownParser('example.md')
word_content = parser.read_word_from_line(25, 5)
```

### `markdown_to_html(self, markdown_text, output_filename=None)`

- Description: Converts Markdown text to HTML format.
- Parameters:
- `markdown_text`: A string representing the Markdown content to convert to HTML.
- `output_filename` (optional): A string representing the filename to save the HTML output. If not provided, the function will only return the HTML string.
- Returns: The Markdown content converted to HTML as a string.
- Usage:

```python
parser = MarkdownParser()
html_output = parser.markdown_to_html('# Heading 1\n\n**Bold Text**')
```

Note: To use most of the functions that read from the Markdown file (e.g., `read_line`, `read_word`, etc.), make sure to initialize the `MarkdownParser` instance with a valid Markdown file using the `filename` parameter. Otherwise, the `markdown_text` will be an empty string.
## Conclusion
This file provides test content to validate the MarkdownParser functionalities, including new features.
113 changes: 55 additions & 58 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
from markdown_worker import MarkdownParser

# Create an instance of the MarkdownParser class and provide a Markdown file (replace 'example.md' with your file name).
parser = MarkdownParser(filename='example.md')

# Read the entire contents of the Markdown file.
markdown_text = parser.read_complete_file()
print("Markdown Content:")
print(markdown_text)

# Extract headers and paragraphs from the Markdown file.
headers, paragraphs, header_indices = parser.extract_headers_and_paragraphs()
print("\nHeaders:")
print(headers)
print("\nParagraphs:")
print(paragraphs)

# Search for a specific heading in the Markdown file.
search_heading = "Introduction"
result = parser.search_heading(search_heading)
print(f"\nContent of '{search_heading}' heading:")
print(result)

# Read a specific line from the Markdown file.
line_number = 2
line_content = parser.read_line(line_number)
print(f"\nContent of line {line_number}:")
print(line_content)

# Read a specific word from a specific line in the Markdown file.
line_number = 3
word_number = 1
word_content = parser.read_word(line_number, word_number)
print(f"\nWord at line {line_number}, word {word_number}:")
print(word_content)

# Read a specific character from a specific word in a specific line in the Markdown file.
line_number = 4
word_number = 0
character_number = 2
character_content = parser.read_character(line_number, word_number, character_number)
print(f"\nCharacter at line {line_number}, word {word_number}, character {character_number}:")
print(character_content)

# Read a specific character from a specific line in the Markdown file.
line_number = 5
character_number = 3
character_content = parser.read_character_from_line(line_number, character_number)
print(f"\nCharacter at line {line_number}, character {character_number}:")
print(character_content)

# Read a specific character from the entire Markdown file.
character_number = 10
character_content = parser.read_character_from_file(character_number)
print(f"\nCharacter at index {character_number}:")
print(character_content)

# Convert the Markdown text to HTML.
html_output = parser.markdown_to_html(markdown_text)
print("\nHTML Output:")

parser = MarkdownParser(filename="example.md")


test_url = "https://example.com/sample.md"
fetch_result = parser.fetch_markdown_from_url(test_url)
print("\nFetching Markdown from URL:")
print(fetch_result)


backup_result = parser.backup_markdown_file("example_backup.md")
print("\nBackup Result:")
print(backup_result)


stats = parser.get_statistics()
print("\nMarkdown File Statistics:")
print(stats)


html_output = parser.markdown_to_html()
print("\nConverted HTML:")
print(html_output)


blockquote_test = parser.search_heading("Blockquote Example")
print("\nBlockquote Test:")
print(blockquote_test)


hr_test = parser.search_heading("Horizontal Rule Example")
print("\nHorizontal Rule Test:")
print(hr_test)


table_test = parser.search_heading("Table Example")
print("\nTable Test:")
print(table_test)


image_test = parser.search_heading("Image Example")
print("\nImage Test:")
print(image_test)


link_test = parser.search_heading("Link Example")
print("\nLink Test:")
print(link_test)


replace_result = parser.find_and_replace("OpenAI", "Markdown Parser")
print("\nFind and Replace Test:")
print(replace_result)


parser.preview_html("test_preview.html")
Loading

0 comments on commit 545a4f6

Please sign in to comment.