LeXtract.FormatHandler (lextract v0.1.2)

View Source

Handles detection and parsing of different text formats (JSON, YAML).

Supports both fenced and unfenced formats:

  • Fenced: ```json ... ```
  • Unfenced: Direct JSON/YAML text

Examples

iex> json = ~s({"key": "value"})
iex> LeXtract.FormatHandler.parse(json, :json)
{:ok, %{"key" => "value"}}

iex> yaml = "key: value"
iex> LeXtract.FormatHandler.parse(yaml, :yaml)
{:ok, %{"key" => "value"}}

Summary

Functions

Extracts content from code fences if present, otherwise returns text unchanged.

Detects if text contains code fences for the given format.

Creates a new format handler.

Parses text in the specified format.

Validates that text is parseable in the given format.

Types

format()

@type format() :: :json | :yaml

parse_result()

@type parse_result() :: {:ok, term()} | {:error, Exception.t()}

t()

@type t() :: %LeXtract.FormatHandler{
  attribute_suffix: String.t(),
  fence_output: boolean(),
  format: format()
}

Functions

extract_fenced_content(text, format)

@spec extract_fenced_content(String.t(), format()) :: String.t()

Extracts content from code fences if present, otherwise returns text unchanged.

Examples

iex> fenced = ~s(```json\n{"key": "value"}\n```)
iex> LeXtract.FormatHandler.extract_fenced_content(fenced, :json)
~s({"key": "value"})

iex> unfenced = ~s({"key": "value"})
iex> LeXtract.FormatHandler.extract_fenced_content(unfenced, :json)
~s({"key": "value"})

iex> fenced_yaml = ~s(```yaml\nkey: value\n```)
iex> LeXtract.FormatHandler.extract_fenced_content(fenced_yaml, :yaml)
"key: value"

fenced?(text, format)

@spec fenced?(String.t(), format()) :: boolean()

Detects if text contains code fences for the given format.

Examples

iex> LeXtract.FormatHandler.fenced?(~s(```json\n{}\n```), :json)
true

iex> LeXtract.FormatHandler.fenced?(~s({}), :json)
false

iex> LeXtract.FormatHandler.fenced?(~s(```yaml\nkey: value\n```), :yaml)
true

iex> LeXtract.FormatHandler.fenced?(~s(```yml\nkey: value\n```), :yaml)
true

new(format, opts \\ [])

@spec new(
  format(),
  keyword()
) :: t()

Creates a new format handler.

Parameters

  • format - Format type (:json or :yaml)
  • opts - Options

Options

  • :fence_output - Whether output should be fenced (default: false)
  • :attribute_suffix - Suffix for attribute fields (default: "_attributes")

Examples

iex> handler = LeXtract.FormatHandler.new(:json)
iex> handler.format
:json

iex> handler = LeXtract.FormatHandler.new(:yaml, fence_output: true)
iex> handler.fence_output
true

parse(text, format)

@spec parse(String.t(), format()) :: parse_result()

Parses text in the specified format.

Automatically detects and removes code fences if present.

Examples

iex> json = ~s({"name": "John"})
iex> {:ok, data} = LeXtract.FormatHandler.parse(json, :json)
iex> data["name"]
"John"

iex> fenced_json = """
...> ```json
...> {"value": 42}
...> ```
...> """
iex> {:ok, data} = LeXtract.FormatHandler.parse(fenced_json, :json)
iex> data["value"]
42

valid?(text, format)

@spec valid?(String.t(), format()) :: boolean()

Validates that text is parseable in the given format.

Examples

iex> LeXtract.FormatHandler.valid?(~s({"key": "value"}), :json)
true

iex> LeXtract.FormatHandler.valid?("{invalid json}", :json)
false

iex> LeXtract.FormatHandler.valid?("key: value", :yaml)
true