Class: Mathpix::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/mathpix/document.rb

Overview

Document processing builder (PDF, DOCX, PPTX)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, document_path) ⇒ Document

Returns a new instance of Document.



8
9
10
11
12
# File 'lib/mathpix/document.rb', line 8

def initialize(client, document_path)
  @client = client
  @document_path = document_path
  @options = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/mathpix/document.rb', line 6

def client
  @client
end

#document_pathObject (readonly)

Returns the value of attribute document_path.



6
7
8
# File 'lib/mathpix/document.rb', line 6

def document_path
  @document_path
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/mathpix/document.rb', line 6

def options
  @options
end

Instance Method Details

#convertDocumentConversion Also known as: call, run

Execute document conversion (async operation).

The whole document is uploaded in a single request — the Mathpix /v3/pdf endpoint paginates large PDFs server-side.

Examples:

conversion = Mathpix::Document.new(client, 'paper.pdf')
  .with_formats(:markdown, :latex)
  .convert
conversion.wait_until_complete
conversion.save_markdown('output.md')

Returns:



83
84
85
86
87
88
89
90
91
# File 'lib/mathpix/document.rb', line 83

def convert
  doc_type = detect_document_type
  conversion_id = client.convert_document(
    document_path: document_path,
    document_type: doc_type,
    **options
  )
  DocumentConversion.new(client, conversion_id, document_path, doc_type)
end

#pages(start_page, end_page = nil) ⇒ self

Set page range for processing

Parameters:

  • start_page (Integer)

    first page (1-indexed)

  • end_page (Integer, nil) (defaults to: nil)

    last page (nil = all)

Returns:

  • (self)


66
67
68
69
# File 'lib/mathpix/document.rb', line 66

def pages(start_page, end_page = nil)
  @options[:page_ranges] = { start: start_page, end: end_page }
  self
end

#quality(level) ⇒ self

Set quality level

Parameters:

  • level (Symbol)

    :low, :medium, :high

Returns:

  • (self)


43
44
45
46
# File 'lib/mathpix/document.rb', line 43

def quality(level)
  @options[:quality] = level
  self
end

#with_diagramsself

Enable diagram extraction

Returns:

  • (self)


35
36
37
38
# File 'lib/mathpix/document.rb', line 35

def with_diagrams
  @options[:include_diagram_svg] = true
  self
end

#with_formats(*formats) ⇒ self

Set output formats

Examples:

doc.with_formats(:markdown, :latex, :docx)

Parameters:

  • formats (Array<Symbol>)

    format names

Returns:

  • (self)


19
20
21
22
# File 'lib/mathpix/document.rb', line 19

def with_formats(*formats)
  @options[:formats] = formats.flatten
  self
end

#with_line_dataself

Enable line-level data (bounding boxes)

Returns:

  • (self)


50
51
52
53
# File 'lib/mathpix/document.rb', line 50

def with_line_data
  @options[:include_line_data] = true
  self
end

#with_tables(**options) ⇒ self

Enable table extraction

Parameters:

  • options (Hash)

    table options

Returns:

  • (self)


27
28
29
30
31
# File 'lib/mathpix/document.rb', line 27

def with_tables(**options)
  @options[:include_table_html] = true
  @options.merge!(options)
  self
end

#with_word_dataself

Enable word-level data (bounding boxes)

Returns:

  • (self)


57
58
59
60
# File 'lib/mathpix/document.rb', line 57

def with_word_data
  @options[:include_word_data] = true
  self
end