Module: Anydoc
- Defined in:
- lib/anydoc.rb,
lib/anydoc/errors.rb,
lib/anydoc/version.rb,
lib/anydoc/document.rb,
sig/anydoc.rbs
Overview
Convert documents (Word, PowerPoint, Excel, OpenDocument, RTF, EPUB, CSV, and PDF) to GitHub-Flavored Markdown.
Anydoc.to_markdown("report.docx")
Every format parses into one shared document model and renders through a single Markdown serializer, so headings, tables, lists, and footnotes come out the same no matter which format goes in.
Defined Under Namespace
Modules: Native Classes: Asset, Block, Cell, CellSlot, ConvertError, Document, EncryptedError, Error, ImageSource, Inline, LinkTarget, List, ListItem, MalformedError, MissingPartError, Note, ResourceLimitError, Style, Table, UnsupportedError
Constant Summary collapse
- VERSION =
The gem version, which is the version of the anydoc crate it wraps.
"0.1.8"- FORMATS =
Every format anydoc reads, named after the extension that identifies it.
Class Method Summary collapse
-
.format_from_bytes(data) ⇒ Symbol?
Detect the format from the content itself: the signature and identity each container specification designates (PDF header, RTF open group, OLE stream names, ZIP package mimetype/content types).
-
.format_from_extension(extension) ⇒ Symbol?
The format an extension names, with or without a leading dot, matched case-insensitively.
-
.format_from_path(path) ⇒ Symbol?
The format a path's extension names.
-
.to_document(data, format: nil) ⇒ Document
Parse an in-memory document into the document model, which also carries the embedded assets.
-
.to_markdown(path) ⇒ String
Convert a document file to Markdown.
-
.to_markdown_bytes(data, format: nil) ⇒ String
Convert an in-memory document to Markdown.
Class Method Details
.format_from_bytes(data) ⇒ Symbol?
Detect the format from the content itself: the signature and identity each container specification designates (PDF header, RTF open group, OLE stream names, ZIP package mimetype/content types).
93 94 95 |
# File 'lib/anydoc.rb', line 93 def format_from_bytes(data) Native.format_from_bytes(data) end |
.format_from_extension(extension) ⇒ Symbol?
The format an extension names, with or without a leading dot, matched case-insensitively.
102 103 104 |
# File 'lib/anydoc.rb', line 102 def format_from_extension(extension) Native.format_from_extension(extension.to_s.delete_prefix(".")) end |
.format_from_path(path) ⇒ Symbol?
The format a path's extension names.
111 112 113 114 |
# File 'lib/anydoc.rb', line 111 def format_from_path(path) extension = File.extname(File.path(path)) extension.empty? ? nil : format_from_extension(extension) end |
.to_document(data, format: nil) ⇒ Document
Parse an in-memory document into the document model, which also carries the embedded assets.
Unsupported for :pdf: PDF conversion produces Markdown directly and
has no document-model form; use to_markdown_bytes.
82 83 84 |
# File 'lib/anydoc.rb', line 82 def to_document(data, format: nil) Native.to_document(data, format_symbol(format)) end |
.to_markdown(path) ⇒ String
Convert a document file to Markdown. The format is detected from the file content; the extension is the fallback for signature-less formats (CSV) and unrecognizable containers.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/anydoc.rb', line 45 def to_markdown(path) path = File.path(path) data = File.binread(path) format = format_from_bytes(data) || format_from_path(path) unless format raise UnsupportedError, "unsupported input: unrecognized file content and extension: #{path}" end to_markdown_bytes(data, format: format) end |
.to_markdown_bytes(data, format: nil) ⇒ String
Convert an in-memory document to Markdown.
66 67 68 |
# File 'lib/anydoc.rb', line 66 def to_markdown_bytes(data, format: nil) Native.to_markdown_bytes(data, format_symbol(format)) end |