Tools in mercury.graph.evidence.formats
Intention
In order to provide a framework that can seriously (i.e., handle a wikipedia dump with 25 million pages) manage sources, we need many tools. The tools in this sub-sub-module are PoC implementations that allow us to build on top of the whole idea.
Warning:
These implementations are vibe-coded. Ideally, if enough people want to build a community around this, we will have owners of different parts and make these tools as good as they should be. For now, they are just a starting point pushing the problem of file conversion to a precise place while still providing something that works well enough to be used as a PoC.
Reference
mercury.graph.evidence.formats.WikiMarkdownWriter(text)
Converts the useful MediaWiki wikitext in a Wikipedia page to Markdown.
(See Warning about the limitations of this conversion.)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Wikitext to convert. None is treated as an empty page. |
required |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
13 14 | |
_normalize_lines(lines)
Collapses excess blank lines while retaining Markdown block separation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list of str
|
Rendered Markdown lines. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Normalized Markdown ending in one newline, or an empty string. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
_remove_templates(text)
Removes nested templates and parser functions, which do not have Markdown equivalents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Wikitext that may contain |
required |
Returns:
| Type | Description |
|---|---|
str
|
Wikitext with templates removed. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
_render_inline(text)
Converts inline MediaWiki markup to Markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Inline wikitext. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown inline content. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
_render_line(line)
Converts headings, lists, links, emphasis, and HTML-like markup in one line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
One wikitext line. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The corresponding Markdown line. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
_render_table(lines)
Renders the contents of one MediaWiki table block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list of str
|
Lines between |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
A Markdown table, or readable fallback lines when no cells exist. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
_render_tables(lines)
Converts MediaWiki table blocks in lines to GitHub-flavored Markdown tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list of str
|
Source lines. |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Lines with each complete table replaced by Markdown lines. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
_table_cell(cell)
Removes MediaWiki cell attributes and renders one table cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell
|
str
|
A source table cell, optionally prefixed by attributes and |
required |
Returns:
| Type | Description |
|---|---|
str
|
Escaped Markdown cell content. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
render()
Returns the page wikitext as a clean Markdown document body.
Returns:
| Type | Description |
|---|---|
str
|
Markdown without an article title heading. |
Source code in mercury/graph/evidence/formats/wiki_markdown_writer.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
mercury.graph.evidence.formats.PdfToMarkdown(src, dst, extra_args=None)
Bases: ABC
The abstract base class for converting a PDF file to Markdown.
To support every possible technology, (oss, proprietary, cloud, etc.) this is built around custom descendants of this class.
There are multiple classes supporting different technologies already implemented. Build a new Endpoint using the mge client to
see the different implementations: MDbyDocling, MDbyPyMuPDF4LLM, MDbyUnlimitedOCR, ...
The simple baseline is MDbyPdfOxide and is part of the mercury-graph library (the rest are just tutorials). It is built around pdf-oxide. To use it, you need to install pdf-oxide since it is not a requirement.
Run pip install pdf-oxide to install it.
This baseline is extremely fast and good with text-based PDFs, but other converters may cover a wider range of PDFs and capture more details especially with graphic content.
To make the Source use custom PdfToMarkdown descendants
- Write a new class just like in the .py examples you will find when creating a new Endpoint with the
mgeclient. - Include it in the configuration of your Source in a similar way as this
"pdf_to_markdown": { "class_name": "MDbyDocling", "$path": "md_by_docling.py", "extra_args": {"do_ocr": false} },
Using converters
The usage is done in three steps:
- Constructing the PdfToMarkdown descendant object. This just stores the paths and extra arguments.
- Verifying that the converter is ready by reading the
readyproperty. There will be no conversion if this is False. This checks, files, dependencies, and server availability, whatever is needed for running the converter. - Calling the
run()method to perform the conversion. This actually writes the output file.
Using other document formats than PDF as the source
The SourceMaker type 'pdf_mirror' can actually work with whatever document format you wish since it does not read the file.
It just maps the file to a corresponding Markdown file. The PdfToMarkdown descendant does the conversion and since it is a custom
class, there is no restriction on the source file format. We just use the word "PDF" out of habit since it is the lingua franca of
document formats. Some of the libraries we give as examples already include support for Word, Excel, PowerPoint, and HWP/HWPX
(E.g., pymupdfpro instead pymupdf4llm from https://github.com/pymupdf/pymupdf4llm) Only the destination file is format is important,
which is Markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
str
|
The path to the source PDF file. |
required |
dst
|
str
|
The path to the destination Markdown file. |
required |
extra_args
|
dict
|
Any extra arguments needed for the conversion. Defaults to None. |
None
|
Source code in mercury/graph/evidence/formats/pdf_to_markdown.py
58 59 60 61 | |
ready
property
Check if the converter is ready to run.
_ready()
abstractmethod
Check if the converter is ready to run, meaning: the dependencies can be imported, the server (if required) is running, and the source file exists.
Returns:
| Type | Description |
|---|---|
bool
|
True if the converter is ready to run, False otherwise. |
Source code in mercury/graph/evidence/formats/pdf_to_markdown.py
83 84 85 86 87 88 89 90 91 92 | |
run()
abstractmethod
Convert the PDF file to Markdown.
This is the actual file conversion method. It reads self.src and writes to self.dst. It may use self.extra for any extra arguments.
Returns:
| Type | Description |
|---|---|
bool
|
True if the conversion was successful, False otherwise. |
Source code in mercury/graph/evidence/formats/pdf_to_markdown.py
70 71 72 73 74 75 76 77 78 79 80 | |