Skip to content

Commit 545a4f6

Browse files
v0.0.4 Release
1 parent 0dd2484 commit 545a4f6

File tree

4 files changed

+175
-317
lines changed

4 files changed

+175
-317
lines changed

example.md

+30-155
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,39 @@
1-
# Markdown Worker Examples
1+
# Example Markdown File
22

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

5-
## Class: MarkdownParser
6+
## Features
7+
- Reads Markdown files
8+
- Extracts headers and paragraphs
9+
- Searches for specific headings
10+
- Converts Markdown to HTML
11+
- Fetches Markdown from URLs
12+
- Supports advanced formatting (blockquotes, horizontal rules, tables, and images)
13+
- Provides interactive CLI mode
614

7-
### `__init__(self, filename=None)`
15+
## Code Example
16+
```python
17+
print("Hello, World!")
18+
```
819

9-
- Description: Constructor for the `MarkdownParser` class.
10-
- Parameters:
11-
- `filename` (optional): A string representing the filename of the Markdown file to read.
12-
- Usage:
20+
## Blockquote Example
21+
> This is a blockquote example.
1322
14-
```python
15-
parser = MarkdownParser('example.md')
16-
```
23+
## Horizontal Rule Example
24+
---
1725

18-
### `read_markdown_file(self)`
26+
## Table Example
27+
| Header 1 | Header 2 |
28+
|----------|----------|
29+
| Data 1 | Data 2 |
30+
| Data 3 | Data 4 |
1931

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

24-
```python
25-
parser = MarkdownParser('example.md')
26-
content = parser.read_markdown_file()
27-
```
35+
## Link Example
36+
[Mantresh](https://www.mantreshkhurana.com)
2837

29-
### `extract_headers_and_paragraphs(self)`
30-
31-
- Description: Extracts headers and paragraphs from the Markdown file.
32-
- Returns:
33-
- `headers`: A list of strings containing the header titles.
34-
- `paragraphs`: A list of strings containing the content of paragraphs.
35-
- `header_indices`: A list of integers representing the line numbers of the headers in the Markdown file.
36-
- Usage:
37-
38-
```python
39-
parser = MarkdownParser('example.md')
40-
headers, paragraphs, header_indices = parser.extract_headers_and_paragraphs()
41-
```
42-
43-
### `search_heading(self, heading_to_search)`
44-
45-
- Description: Searches for a specific header in the Markdown file and returns its content.
46-
- Parameters:
47-
- `heading_to_search`: A string representing the header title to search for.
48-
- Returns: The content of the header as a string.
49-
- Usage:
50-
51-
```python
52-
parser = MarkdownParser('example.md')
53-
heading_content = parser.search_heading('Introduction')
54-
```
55-
56-
### `read_complete_file(self)`
57-
58-
- Description: Returns the complete content of the Markdown file.
59-
- Returns: The content of the entire Markdown file as a string.
60-
- Usage:
61-
62-
```python
63-
parser = MarkdownParser('example.md')
64-
complete_content = parser.read_complete_file()
65-
```
66-
67-
### `read_line(self, line_number)`
68-
69-
- Description: Reads a specific line from the Markdown file.
70-
- Parameters:
71-
- `line_number`: An integer representing the line number to read (0-based index).
72-
- Returns: The content of the specified line as a string.
73-
- Usage:
74-
75-
```python
76-
parser = MarkdownParser('example.md')
77-
line_content = parser.read_line(5)
78-
```
79-
80-
### `read_word(self, line_number, word_number)`
81-
82-
- Description: Reads a specific word from a given line in the Markdown file.
83-
- Parameters:
84-
- `line_number`: An integer representing the line number (0-based index) to read.
85-
- `word_number`: An integer representing the word number (0-based index) to read.
86-
- Returns: The specified word from the specified line as a string.
87-
- Usage:
88-
89-
```python
90-
parser = MarkdownParser('example.md')
91-
word_content = parser.read_word(10, 3)
92-
```
93-
94-
### `read_character(self, line_number, word_number, character_number)`
95-
96-
- Description: Reads a specific character from a given word in a given line in the Markdown file.
97-
- Parameters:
98-
- `line_number`: An integer representing the line number (0-based index) to read.
99-
- `word_number`: An integer representing the word number (0-based index) to read.
100-
- `character_number`: An integer representing the character number (0-based index) to read.
101-
- Returns: The specified character from the specified word in the specified line as a string.
102-
- Usage:
103-
104-
```python
105-
parser = MarkdownParser('example.md')
106-
character = parser.read_character(15, 2, 4)
107-
```
108-
109-
### `read_character_from_line(self, line_number, character_number)`
110-
111-
- Description: Reads a specific character from a given line in the Markdown file.
112-
- Parameters:
113-
- `line_number`: An integer representing the line number (0-based index) to read.
114-
- `character_number`: An integer representing the character number (0-based index) to read.
115-
- Returns: The specified character from the specified line as a string.
116-
- Usage:
117-
118-
```python
119-
parser = MarkdownParser('example.md')
120-
character = parser.read_character_from_line(20, 10)
121-
```
122-
123-
### `read_character_from_file(self, character_number)`
124-
125-
- Description: Reads a specific character from the entire Markdown file.
126-
- Parameters:
127-
- `character_number`: An integer representing the character number (0-based index) to read.
128-
- Returns: The specified character from the Markdown file as a string.
129-
- Usage:
130-
131-
```python
132-
parser = MarkdownParser('example.md')
133-
character = parser.read_character_from_file(50)
134-
```
135-
136-
### `read_word_from_line(self, line_number, word_number)`
137-
138-
- Description: Reads a specific word from a given line in the Markdown file.
139-
- Parameters:
140-
- `line_number`: An integer representing the line number (0-based index) to read.
141-
- `word_number`: An integer representing the word number (0-based index) to read.
142-
- Returns: The specified word from the specified line as a string.
143-
- Usage:
144-
145-
```python
146-
parser = MarkdownParser('example.md')
147-
word_content = parser.read_word_from_line(25, 5)
148-
```
149-
150-
### `markdown_to_html(self, markdown_text, output_filename=None)`
151-
152-
- Description: Converts Markdown text to HTML format.
153-
- Parameters:
154-
- `markdown_text`: A string representing the Markdown content to convert to HTML.
155-
- `output_filename` (optional): A string representing the filename to save the HTML output. If not provided, the function will only return the HTML string.
156-
- Returns: The Markdown content converted to HTML as a string.
157-
- Usage:
158-
159-
```python
160-
parser = MarkdownParser()
161-
html_output = parser.markdown_to_html('# Heading 1\n\n**Bold Text**')
162-
```
163-
164-
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.
38+
## Conclusion
39+
This file provides test content to validate the MarkdownParser functionalities, including new features.

example.py

+55-58
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,58 @@
11
from markdown_worker import MarkdownParser
22

3-
# Create an instance of the MarkdownParser class and provide a Markdown file (replace 'example.md' with your file name).
4-
parser = MarkdownParser(filename='example.md')
5-
6-
# Read the entire contents of the Markdown file.
7-
markdown_text = parser.read_complete_file()
8-
print("Markdown Content:")
9-
print(markdown_text)
10-
11-
# Extract headers and paragraphs from the Markdown file.
12-
headers, paragraphs, header_indices = parser.extract_headers_and_paragraphs()
13-
print("\nHeaders:")
14-
print(headers)
15-
print("\nParagraphs:")
16-
print(paragraphs)
17-
18-
# Search for a specific heading in the Markdown file.
19-
search_heading = "Introduction"
20-
result = parser.search_heading(search_heading)
21-
print(f"\nContent of '{search_heading}' heading:")
22-
print(result)
23-
24-
# Read a specific line from the Markdown file.
25-
line_number = 2
26-
line_content = parser.read_line(line_number)
27-
print(f"\nContent of line {line_number}:")
28-
print(line_content)
29-
30-
# Read a specific word from a specific line in the Markdown file.
31-
line_number = 3
32-
word_number = 1
33-
word_content = parser.read_word(line_number, word_number)
34-
print(f"\nWord at line {line_number}, word {word_number}:")
35-
print(word_content)
36-
37-
# Read a specific character from a specific word in a specific line in the Markdown file.
38-
line_number = 4
39-
word_number = 0
40-
character_number = 2
41-
character_content = parser.read_character(line_number, word_number, character_number)
42-
print(f"\nCharacter at line {line_number}, word {word_number}, character {character_number}:")
43-
print(character_content)
44-
45-
# Read a specific character from a specific line in the Markdown file.
46-
line_number = 5
47-
character_number = 3
48-
character_content = parser.read_character_from_line(line_number, character_number)
49-
print(f"\nCharacter at line {line_number}, character {character_number}:")
50-
print(character_content)
51-
52-
# Read a specific character from the entire Markdown file.
53-
character_number = 10
54-
character_content = parser.read_character_from_file(character_number)
55-
print(f"\nCharacter at index {character_number}:")
56-
print(character_content)
57-
58-
# Convert the Markdown text to HTML.
59-
html_output = parser.markdown_to_html(markdown_text)
60-
print("\nHTML Output:")
3+
4+
parser = MarkdownParser(filename="example.md")
5+
6+
7+
test_url = "https://example.com/sample.md"
8+
fetch_result = parser.fetch_markdown_from_url(test_url)
9+
print("\nFetching Markdown from URL:")
10+
print(fetch_result)
11+
12+
13+
backup_result = parser.backup_markdown_file("example_backup.md")
14+
print("\nBackup Result:")
15+
print(backup_result)
16+
17+
18+
stats = parser.get_statistics()
19+
print("\nMarkdown File Statistics:")
20+
print(stats)
21+
22+
23+
html_output = parser.markdown_to_html()
24+
print("\nConverted HTML:")
6125
print(html_output)
26+
27+
28+
blockquote_test = parser.search_heading("Blockquote Example")
29+
print("\nBlockquote Test:")
30+
print(blockquote_test)
31+
32+
33+
hr_test = parser.search_heading("Horizontal Rule Example")
34+
print("\nHorizontal Rule Test:")
35+
print(hr_test)
36+
37+
38+
table_test = parser.search_heading("Table Example")
39+
print("\nTable Test:")
40+
print(table_test)
41+
42+
43+
image_test = parser.search_heading("Image Example")
44+
print("\nImage Test:")
45+
print(image_test)
46+
47+
48+
link_test = parser.search_heading("Link Example")
49+
print("\nLink Test:")
50+
print(link_test)
51+
52+
53+
replace_result = parser.find_and_replace("OpenAI", "Markdown Parser")
54+
print("\nFind and Replace Test:")
55+
print(replace_result)
56+
57+
58+
parser.preview_html("test_preview.html")

0 commit comments

Comments
 (0)