Class: Kotoshu::Documents::PlainTextDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/kotoshu/documents/plain_text_document.rb

Overview

Built-in Document parser for plain text (no markup).

The only parser kotoshu ships. Format-specific parsers (Markdown, AsciiDoc, etc.) live in plugins — see register.

PlainTextDocument is intentionally simple: one TextNode covering the entire source. The flattened text equals the source, so source-to-flattened mapping is the identity.

Instance Attribute Summary

Attributes inherited from Document

#format, #language_code, #source, #text_nodes

Class Method Summary collapse

Methods inherited from Document

#each_node, #flattened_length, #flattened_offset?, #flattened_text, #initialize, #source_range_at, #source_range_for

Constructor Details

This class inherits a constructor from Kotoshu::Documents::Document

Class Method Details

.from_file(path, language_code: nil) ⇒ PlainTextDocument

Build a document from a file.

Parameters:

  • path (String)
  • language_code (String, nil) (defaults to: nil)

Returns:



36
37
38
# File 'lib/kotoshu/documents/plain_text_document.rb', line 36

def self.from_file(path, language_code: nil)
  from_string(File.read(path), language_code: language_code)
end

.from_string(text, language_code: nil) ⇒ PlainTextDocument

Build a document from a string.

Parameters:

  • text (String)
  • language_code (String, nil) (defaults to: nil)

Returns:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kotoshu/documents/plain_text_document.rb', line 19

def self.from_string(text, language_code: nil)
  text ||= ""
  range = source_range_for_string(text, 0)
  new(
    text_nodes: [TextNode.new(text: text, source_range: range,
                              flattened_offset: 0, format: :plain, metadata: {})],
    source: text,
    format: :plain,
    language_code: language_code
  )
end