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
- Variables
- func GenerateAnchorString(label string) string
- type ConditionalAttrs
- type Document
- func (d *Document) AllNodes() iter.Seq[Node]
- func (d *Document) AllNodesRecursive() iter.Seq[Node]
- func (d *Document) CollectNodesOfType(nodeType string) []Node
- func (d *Document) CountNodesOfType(nodeType string) int
- func (d *Document) HasNodeOfType(nodeType string) bool
- func (d *Document) NodesOfType(nodeType string) iter.Seq2[int, Node]
- func (d *Document) Serialize() (json.RawMessage, error)
- func (d *Document) SerializeIndented() (json.RawMessage, error)
- type ExportInfo
- type InjectorAttrs
- type LogicGroup
- type LogicRule
- type Margins
- type Mark
- type Meta
- type Node
- type PageConfig
- type ProseMirrorDoc
- type RuleValue
- type Set
- func (s Set[T]) Add(item T)
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(item T) bool
- func (s Set[T]) Difference(other Set[T]) Set[T]
- func (s Set[T]) Intersection(other Set[T]) Set[T]
- func (s Set[T]) Len() int
- func (s Set[T]) Remove(item T)
- func (s Set[T]) ToSlice() []T
- func (s Set[T]) Union(other Set[T]) Set[T]
Constants ¶
const ( LogicTypeGroup = "group" LogicTypeRule = "rule" )
Logic type constants.
const ( LogicAND = "AND" LogicOR = "OR" )
Logic operator constants.
const ( RuleModeText = "text" RuleModeVariable = "variable" )
Rule value mode constants.
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.
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.
const ( MarkTypeBold = "bold" MarkTypeItalic = "italic" MarkTypeStrike = "strike" MarkTypeCode = "code" MarkTypeUnderline = "underline" MarkTypeHighlight = "highlight" MarkTypeLink = "link" )
Mark type constants.
const ( InjectorTypeText = "TEXT" InjectorTypeNumber = "NUMBER" InjectorTypeDate = "DATE" InjectorTypeCurrency = "CURRENCY" InjectorTypeBoolean = "BOOLEAN" InjectorTypeImage = "IMAGE" InjectorTypeTable = "TABLE" )
Injector type constants.
const ( LanguageEnglish = "en" LanguageSpanish = "es" )
Language constants.
const ( PageFormatA4 = "A4" PageFormatLetter = "LETTER" PageFormatLegal = "LEGAL" PageFormatCustom = "CUSTOM" )
Page format constants.
const CurrentVersion = "1.1.0"
CurrentVersion is the latest supported format version.
const MaxNestingDepth = 3
MaxNestingDepth is the maximum allowed nesting depth for condition groups.
Variables ¶
var NoValueOperators = Set[string]{ OpEmpty: {}, OpNotEmpty: {}, OpIsTrue: {}, OpIsFalse: {}, }
NoValueOperators are operators that don't require a value.
var ValidInjectorTypes = Set[string]{ InjectorTypeText: {}, InjectorTypeNumber: {}, InjectorTypeDate: {}, InjectorTypeCurrency: {}, InjectorTypeBoolean: {}, InjectorTypeImage: {}, InjectorTypeTable: {}, }
ValidInjectorTypes contains allowed injector types.
var ValidLanguages = Set[string]{ LanguageEnglish: {}, LanguageSpanish: {}, }
ValidLanguages contains allowed language codes.
var ValidLogicOperators = Set[string]{ LogicAND: {}, LogicOR: {}, }
ValidLogicOperators contains allowed logic operators.
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.
var ValidPageFormats = Set[string]{ PageFormatA4: {}, PageFormatLetter: {}, PageFormatLegal: {}, PageFormatCustom: {}, }
ValidPageFormats contains allowed page format IDs.
var ValidRuleModes = Set[string]{ RuleModeText: {}, RuleModeVariable: {}, }
ValidRuleModes contains allowed rule value modes.
Functions ¶
func GenerateAnchorString ¶
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 ¶
AllNodes returns an iterator over all nodes in the document using BFS traversal.
func (*Document) AllNodesRecursive ¶
AllNodesRecursive returns an iterator over all nodes using DFS traversal.
func (*Document) CollectNodesOfType ¶
CollectNodesOfType collects all nodes of a specific type into a slice.
func (*Document) CountNodesOfType ¶
CountNodesOfType counts nodes of a specific type.
func (*Document) HasNodeOfType ¶
HasNodeOfType checks if document has at least one node of the specified type.
func (*Document) NodesOfType ¶
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 ¶
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 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 (Set[T]) Difference ¶
Difference returns a new set containing items in s but not in other.
func (Set[T]) Intersection ¶
Intersection returns a new set containing items present in both sets.