portabledoc

package
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package portabledoc defines types for the Portable Document Format (PDF-JSON). This format is used to export, import, and persist contract editor documents.

Index

Constants

View Source
const (
	LogicTypeGroup = "group"
	LogicTypeRule  = "rule"
)

Logic type constants.

View Source
const (
	LogicAND = "AND"
	LogicOR  = "OR"
)

Logic operator constants.

View Source
const (
	RuleModeText     = "text"
	RuleModeVariable = "variable"
)

Rule value mode constants.

View Source
const (
	OpEqual      = "eq"
	OpNotEqual   = "neq"
	OpEmpty      = "empty"
	OpNotEmpty   = "not_empty"
	OpStartsWith = "starts_with"
	OpEndsWith   = "ends_with"
	OpContains   = "contains"
	OpGreater    = "gt"
	OpLess       = "lt"
	OpGreaterEq  = "gte"
	OpLessEq     = "lte"
	OpBefore     = "before"
	OpAfter      = "after"
	OpIsTrue     = "is_true"
	OpIsFalse    = "is_false"
)

Comparison operator constants.

View Source
const (
	NodeTypeDoc         = "doc"
	NodeTypeParagraph   = "paragraph"
	NodeTypeHeading     = "heading"
	NodeTypeBlockquote  = "blockquote"
	NodeTypeCodeBlock   = "codeBlock"
	NodeTypeHR          = "horizontalRule"
	NodeTypeBulletList  = "bulletList"
	NodeTypeOrderedList = "orderedList"
	NodeTypeTaskList    = "taskList"
	NodeTypeListItem    = "listItem"
	NodeTypeTaskItem    = "taskItem"
	NodeTypeInjector    = "injector"
	NodeTypeConditional = "conditional"
	NodeTypePageBreak   = "pageBreak"
	NodeTypeImage       = "image"
	NodeTypeCustomImage = "customImage"
	NodeTypeText        = "text"
	// List types
	NodeTypeListInjector = "listInjector" // Dynamic list from system injector
	// Table types
	NodeTypeTableInjector = "tableInjector" // Dynamic table from system injector
	NodeTypeTable         = "table"         // Editable table (user-created)
	NodeTypeTableRow      = "tableRow"
	NodeTypeTableCell     = "tableCell"
	NodeTypeTableHeader   = "tableHeader"
)

Node type constants.

View Source
const (
	MarkTypeBold      = "bold"
	MarkTypeItalic    = "italic"
	MarkTypeStrike    = "strike"
	MarkTypeCode      = "code"
	MarkTypeUnderline = "underline"
	MarkTypeHighlight = "highlight"
	MarkTypeLink      = "link"
)

Mark type constants.

View Source
const (
	InjectorTypeText     = "TEXT"
	InjectorTypeNumber   = "NUMBER"
	InjectorTypeDate     = "DATE"
	InjectorTypeCurrency = "CURRENCY"
	InjectorTypeBoolean  = "BOOLEAN"
	InjectorTypeImage    = "IMAGE"
	InjectorTypeTable    = "TABLE"
)

Injector type constants.

View Source
const (
	LanguageEnglish = "en"
	LanguageSpanish = "es"
)

Language constants.

View Source
const (
	PageFormatA4     = "A4"
	PageFormatLetter = "LETTER"
	PageFormatLegal  = "LEGAL"
	PageFormatCustom = "CUSTOM"
)

Page format constants.

View Source
const CurrentVersion = "1.1.0"

CurrentVersion is the latest supported format version.

View Source
const MaxNestingDepth = 3

MaxNestingDepth is the maximum allowed nesting depth for condition groups.

Variables

View Source
var NoValueOperators = Set[string]{
	OpEmpty:    {},
	OpNotEmpty: {},
	OpIsTrue:   {},
	OpIsFalse:  {},
}

NoValueOperators are operators that don't require a value.

ValidInjectorTypes contains allowed injector types.

View Source
var ValidLanguages = Set[string]{
	LanguageEnglish: {},
	LanguageSpanish: {},
}

ValidLanguages contains allowed language codes.

View Source
var ValidLogicOperators = Set[string]{
	LogicAND: {},
	LogicOR:  {},
}

ValidLogicOperators contains allowed logic operators.

View Source
var ValidOperators = Set[string]{
	OpEqual:      {},
	OpNotEqual:   {},
	OpEmpty:      {},
	OpNotEmpty:   {},
	OpStartsWith: {},
	OpEndsWith:   {},
	OpContains:   {},
	OpGreater:    {},
	OpLess:       {},
	OpGreaterEq:  {},
	OpLessEq:     {},
	OpBefore:     {},
	OpAfter:      {},
	OpIsTrue:     {},
	OpIsFalse:    {},
}

ValidOperators contains all valid comparison operators.

View Source
var ValidPageFormats = Set[string]{
	PageFormatA4:     {},
	PageFormatLetter: {},
	PageFormatLegal:  {},
	PageFormatCustom: {},
}

ValidPageFormats contains allowed page format IDs.

View Source
var ValidRuleModes = Set[string]{
	RuleModeText:     {},
	RuleModeVariable: {},
}

ValidRuleModes contains allowed rule value modes.

Functions

func GenerateAnchorString

func GenerateAnchorString(label string) string

GenerateAnchorString creates a valid anchor string from a role label. Format: __sig_{sanitized_label}__ This function is used both during template publishing (to store anchors) and during document generation (to match recipients to roles).

Types

type ConditionalAttrs

type ConditionalAttrs struct {
	Conditions LogicGroup `json:"conditions"`
	Expression string     `json:"expression"`
}

ConditionalAttrs represents conditional block attributes.

func ParseConditionalAttrs

func ParseConditionalAttrs(attrs map[string]any) (*ConditionalAttrs, error)

ParseConditionalAttrs parses node attrs into ConditionalAttrs.

type Document

type Document struct {
	Version     string          `json:"version"`
	Meta        Meta            `json:"meta"`
	PageConfig  PageConfig      `json:"pageConfig"`
	VariableIDs []string        `json:"variableIds"`
	Content     *ProseMirrorDoc `json:"content"`
	ExportInfo  ExportInfo      `json:"exportInfo"`
}

Document represents the complete portable document format.

func MustParse

func MustParse(data json.RawMessage) *Document

MustParse parses JSON into Document, panics on error. Useful for tests and initialization with known-good data.

func Parse

func Parse(data json.RawMessage) (*Document, error)

Parse parses JSON into Document. Returns nil, nil if data is empty.

func (*Document) AllNodes

func (d *Document) AllNodes() iter.Seq[Node]

AllNodes returns an iterator over all nodes in the document using BFS traversal.

func (*Document) AllNodesRecursive

func (d *Document) AllNodesRecursive() iter.Seq[Node]

AllNodesRecursive returns an iterator over all nodes using DFS traversal.

func (*Document) CollectNodesOfType

func (d *Document) CollectNodesOfType(nodeType string) []Node

CollectNodesOfType collects all nodes of a specific type into a slice.

func (*Document) CountNodesOfType

func (d *Document) CountNodesOfType(nodeType string) int

CountNodesOfType counts nodes of a specific type.

func (*Document) HasNodeOfType

func (d *Document) HasNodeOfType(nodeType string) bool

HasNodeOfType checks if document has at least one node of the specified type.

func (*Document) NodesOfType

func (d *Document) NodesOfType(nodeType string) iter.Seq2[int, Node]

NodesOfType returns an iterator over nodes of a specific type with their index.

func (*Document) Serialize

func (d *Document) Serialize() (json.RawMessage, error)

Serialize converts Document to JSON.

func (*Document) SerializeIndented

func (d *Document) SerializeIndented() (json.RawMessage, error)

SerializeIndented converts Document to indented JSON.

type ExportInfo

type ExportInfo struct {
	ExportedAt string  `json:"exportedAt"`
	ExportedBy *string `json:"exportedBy,omitempty"`
	SourceApp  string  `json:"sourceApp"`
	Checksum   *string `json:"checksum,omitempty"`
}

ExportInfo contains export metadata.

type InjectorAttrs

type InjectorAttrs struct {
	Type       string  `json:"type"`
	Label      string  `json:"label"`
	VariableID string  `json:"variableId"`
	Format     *string `json:"format,omitempty"`
	Required   *bool   `json:"required,omitempty"`
}

InjectorAttrs represents injector node attributes.

func ParseInjectorAttrs

func ParseInjectorAttrs(attrs map[string]any) (*InjectorAttrs, error)

ParseInjectorAttrs parses node attrs into InjectorAttrs.

type LogicGroup

type LogicGroup struct {
	ID       string `json:"id"`
	Type     string `json:"type"`  // always "group"
	Logic    string `json:"logic"` // "AND" | "OR"
	Children []any  `json:"children"`
}

LogicGroup represents a group of conditions.

func ParseLogicGroup

func ParseLogicGroup(v any) (*LogicGroup, error)

ParseLogicGroup parses any value into LogicGroup.

type LogicRule

type LogicRule struct {
	ID         string    `json:"id"`
	Type       string    `json:"type"` // always "rule"
	VariableID string    `json:"variableId"`
	Operator   string    `json:"operator"`
	Value      RuleValue `json:"value"`
}

LogicRule represents a single condition rule.

func ParseLogicRule

func ParseLogicRule(v any) (*LogicRule, error)

ParseLogicRule parses any value into LogicRule.

type Margins

type Margins struct {
	Top    float64 `json:"top"`
	Bottom float64 `json:"bottom"`
	Left   float64 `json:"left"`
	Right  float64 `json:"right"`
}

Margins defines page margins in pixels.

type Mark

type Mark struct {
	Type  string         `json:"type"`
	Attrs map[string]any `json:"attrs,omitempty"`
}

Mark represents a text mark.

type Meta

type Meta struct {
	Title        string            `json:"title"`
	Description  *string           `json:"description,omitempty"`
	Language     string            `json:"language"` // "en" | "es"
	CustomFields map[string]string `json:"customFields,omitempty"`
}

Meta contains document metadata.

type Node

type Node struct {
	Type    string         `json:"type"`
	Attrs   map[string]any `json:"attrs,omitempty"`
	Content []Node         `json:"content,omitempty"`
	Marks   []Mark         `json:"marks,omitempty"`
	Text    *string        `json:"text,omitempty"`
}

Node represents a node in the document.

type PageConfig

type PageConfig struct {
	FormatID        string  `json:"formatId"` // "A4" | "LETTER" | "LEGAL" | "CUSTOM"
	Width           float64 `json:"width"`
	Height          float64 `json:"height"`
	Margins         Margins `json:"margins"`
	ShowPageNumbers bool    `json:"showPageNumbers"`
	PageGap         float64 `json:"pageGap"`
}

PageConfig contains page configuration.

type ProseMirrorDoc

type ProseMirrorDoc struct {
	Type    string `json:"type"` // always "doc"
	Content []Node `json:"content"`
}

ProseMirrorDoc represents the document content.

type RuleValue

type RuleValue struct {
	Mode  string `json:"mode"` // "text" | "variable"
	Value string `json:"value"`
}

RuleValue represents the value in a rule.

type Set

type Set[T comparable] map[T]struct{}

Set is a generic set implementation using map with empty struct values.

func NewSet

func NewSet[T comparable](items []T) Set[T]

NewSet creates a new set from a slice.

func (Set[T]) Add

func (s Set[T]) Add(item T)

Add adds an item to the set.

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone creates a copy of the set.

func (Set[T]) Contains

func (s Set[T]) Contains(item T) bool

Contains checks if item exists in set.

func (Set[T]) Difference

func (s Set[T]) Difference(other Set[T]) Set[T]

Difference returns a new set containing items in s but not in other.

func (Set[T]) Intersection

func (s Set[T]) Intersection(other Set[T]) Set[T]

Intersection returns a new set containing items present in both sets.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the number of items in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(item T)

Remove removes an item from the set.

func (Set[T]) ToSlice

func (s Set[T]) ToSlice() []T

ToSlice converts set to slice.

func (Set[T]) Union

func (s Set[T]) Union(other Set[T]) Set[T]

Union returns a new set containing all items from both sets.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL