jplaw2epub

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 20 Imported by: 0

README

jplaw2epub

Go library and command line tool to convert Japanese Standard Law XML Schema (法令標準XMLスキーマ) into EPUB files.

Overview

jplaw2epub is a comprehensive tool for converting Japanese law documents from the official XML format to EPUB e-books. It supports the full Japanese Standard Law XML Schema specification, including all document structures, appendixes, tables, and figures.

Library Usage

package main

import (
    "fmt"
    "os"
    
    "go.ngs.io/jplaw2epub"
)

func main() {
    // Method 1: Create EPUB from XML file path
    book, err := jplaw2epub.CreateEPUBFromXMLPath("law.xml")
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    if err := jplaw2epub.WriteEPUB(book, "output.epub"); err != nil {
        fmt.Printf("Error writing EPUB: %v\n", err)
        return
    }

    // Method 2: Create EPUB from io.Reader
    xmlFile, err := os.Open("law.xml")
    if err != nil {
        fmt.Printf("Error opening file: %v\n", err)
        return
    }
    defer xmlFile.Close()

    book2, err := jplaw2epub.CreateEPUBFromXMLFile(xmlFile)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    if err := jplaw2epub.WriteEPUB(book2, "output2.epub"); err != nil {
        fmt.Printf("Error writing EPUB: %v\n", err)
        return
    }

    fmt.Println("EPUB files created successfully!")
}
API Functions
  • CreateEPUBFromXMLPath(xmlPath string) (*epub.Epub, error) - Creates an EPUB from a file path
  • CreateEPUBFromXMLFile(xmlFile io.Reader) (*epub.Epub, error) - Creates an EPUB from an io.Reader
  • WriteEPUB(book *epub.Epub, destPath string) error - Writes an EPUB book to a file

Command Line Usage

Installation

Install the CLI tool:

go install go.ngs.io/jplaw2epub/cmd/jplaw2epub@latest

Or build from source:

git clone https://github.com/ngs/jplaw2epub.git
cd jplaw2epub
go build -o jplaw2epub ./cmd/jplaw2epub
Basic Usage
jplaw2epub -d output.epub input.xml
Command Line Options
-d string
    Destination file path (required)
-no-images
    Skip downloading and embedding images
-max-image-height string
    Maximum image height (e.g., '300px', '80vh', '50%') (default "80vh")
Examples

Convert a law XML file to EPUB:

jplaw2epub -d mylaw.epub path/to/law.xml

Convert without downloading images:

jplaw2epub -no-images -d mylaw.epub path/to/law.xml

Convert with custom image height limit:

jplaw2epub -max-image-height "500px" -d mylaw.epub path/to/law.xml

Installation as Go Library

Add to your Go project:

go get go.ngs.io/jplaw2epub

Features

Document Structure Support
  • Main Provisions: Chapters, sections, subsections, divisions, and articles
  • Supplementary Provisions: Full support with chapters, articles, and appendixes
  • Paragraph Hierarchy: Proper handling of numbered and unnumbered paragraphs
  • Item Structure: Support for Items, Subitem1, Subitem2, and Subitem3
  • List Elements: Native list support with proper nesting (List, Sublist1-3)
Appendix Support
  • AppdxTable: Appendix tables with full table structure
  • AppdxNote: Appendix notes with structured content
  • AppdxStyle: Appendix styles with formatting
  • AppdxFormat: Appendix formats with embedded figures
  • AppdxFig: Appendix figures with image support
Advanced Features
  • Ruby Annotations: Full support for Japanese phonetic guides (ルビ)
  • Table Processing: Complex tables with headers, spans, and borders
  • Image Embedding: Automatic download and embedding of referenced images
  • Figure Support: FigStruct and Fig element processing
  • Style Management: StyleStruct and Format element handling
  • Dynamic List Styling: Automatic detection (CJK ideographic, katakana-iroha, hiragana-iroha)
Technical Features
  • High Test Coverage: 71.5% code coverage with comprehensive test suite
  • Clean Code: Passes all Go linters (gofmt, go vet, golangci-lint)
  • Modular Architecture: Well-organized code with separate processors for each element type
  • Error Handling: Robust error handling throughout the conversion process
  • EPUB Metadata: Full support with Japanese era information
  • Clean Go API: Simple library interface for programmatic usage
  • Cross-platform CLI: Command-line tool with binary releases

Data Sources

The tool processes XML files conforming to the Japanese Standard Law XML Schema. You can obtain these files from:

Requirements

  • Go 1.18 or higher (for building from source)
  • XML files conforming to Japanese Standard Law XML Schema

Testing

Run the test suite:

go test -v ./...

Generate coverage report:

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Current test coverage: 71.5%

Development

Building from Source
git clone https://github.com/ngs/jplaw2epub.git
cd jplaw2epub
go build -o jplaw2epub ./cmd/jplaw2epub
Running Linters
# Format code
gofmt -w .

# Check for issues
go vet ./...

# Run comprehensive linting (requires golangci-lint)
golangci-lint run ./...

# Clean up dependencies
go mod tidy
Contributing
  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please ensure:

  • All tests pass (go test ./...)
  • Code coverage remains above 70%
  • All linters pass
  • New features include appropriate tests

Author

Atsushi Nagase

License

MIT License

Copyright © 2025 Atsushi Nagase. See LICENSE.md for details.

Documentation

Overview

Package jplaw2epub converts Japanese Standard Law XML Schema (法令標準XMLスキーマ) into EPUB files.

This package provides functionality to convert Japanese legal documents in XML format into EPUB ebooks with proper formatting, Ruby annotations support, and Japanese-specific list styling.

Basic usage:

book, err := jplaw2epub.CreateEPUBFromXMLPath("law.xml")
if err != nil {
	return err
}
return jplaw2epub.WriteEPUB(book, "output.epub")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCSSToEPUB added in v1.1.0

func AddCSSToEPUB(book *epub.Epub) error

AddCSSToEPUB adds CSS stylesheet to the EPUB

func CreateEPUBFromXMLFile

func CreateEPUBFromXMLFile(xmlFile io.Reader) (*epub.Epub, error)

CreateEPUBFromXMLFile creates an EPUB file from a jplaw XML file reader.

This function reads XML data from the provided io.Reader, parses it as Japanese law data according to the Japanese Standard Law XML Schema, and creates an EPUB book with proper formatting and metadata.

Example:

xmlFile, err := os.Open("law.xml")
if err != nil {
	return err
}
defer xmlFile.Close()

book, err := jplaw2epub.CreateEPUBFromXMLFile(xmlFile)
if err != nil {
	return err
}

func CreateEPUBFromXMLFileWithOptions added in v1.1.0

func CreateEPUBFromXMLFileWithOptions(xmlFile io.Reader, opts *EPUBOptions) (*epub.Epub, error)

CreateEPUBFromXMLFileWithOptions creates an EPUB file with image support

func CreateEPUBFromXMLPath

func CreateEPUBFromXMLPath(xmlPath string) (*epub.Epub, error)

CreateEPUBFromXMLPath creates an EPUB file from a jplaw XML file path.

This is a convenience function that opens the file at the given path and calls CreateEPUBFromXMLFile to process it. Images will be automatically downloaded and embedded if the filename contains a valid revision ID.

Example:

book, err := jplaw2epub.CreateEPUBFromXMLPath("law.xml")
if err != nil {
	return err
}

func CreateEPUBFromXMLPathWithOptions added in v1.1.0

func CreateEPUBFromXMLPathWithOptions(xmlPath string, opts *EPUBOptions) (*epub.Epub, error)

CreateEPUBFromXMLPathWithOptions creates an EPUB file with image support

func ProcessStyleStructs added in v1.1.0

func ProcessStyleStructs(styles []jplaw.StyleStruct, imgProc ImageProcessorInterface) string

ProcessStyleStructs processes multiple StyleStructs

func WriteEPUB

func WriteEPUB(book *epub.Epub, destPath string) error

WriteEPUB writes the EPUB book to the specified path.

The function ensures the directory exists before writing and returns an error if the write operation fails.

Example:

err := jplaw2epub.WriteEPUB(book, "output.epub")
if err != nil {
	return err
}

Types

type APIClient added in v1.2.0

type APIClient interface {
	GetAttachment(lawRevisionID string, params *lawapi.GetAttachmentParams) (*string, error)
}

APIClient defines the interface for the law API client

type EPUBOptions added in v1.1.0

type EPUBOptions struct {
	// APIClient is the jplaw API client for downloading images
	APIClient *lawapi.Client
	// RevisionID is the revision ID for fetching attachments
	RevisionID string
	// MaxImageHeight is the maximum height for images (e.g., "300px", "80vh", "50%")
	MaxImageHeight string
}

EPUBOptions contains options for EPUB creation

type ImageProcessor added in v1.1.0

type ImageProcessor struct {
	// contains filtered or unexported fields
}

ImageProcessor handles image processing for EPUB

func NewImageProcessor added in v1.1.0

func NewImageProcessor(client APIClient, revisionID string, book *epub.Epub) *ImageProcessor

NewImageProcessor creates a new image processor

func (*ImageProcessor) ProcessFigStruct added in v1.1.0

func (ip *ImageProcessor) ProcessFigStruct(fig *jplaw.FigStruct) (string, error)

ProcessFigStruct processes a FigStruct and returns HTML

func (*ImageProcessor) SetMaxImageHeight added in v1.1.0

func (ip *ImageProcessor) SetMaxImageHeight(height string)

SetMaxImageHeight sets the maximum image height

type ImageProcessorInterface added in v1.2.0

type ImageProcessorInterface interface {
	ProcessFigStruct(fig *jplaw.FigStruct) (string, error)
	SetMaxImageHeight(height string)
}

ImageProcessorInterface defines the interface for image processing

type StyleProcessor added in v1.1.0

type StyleProcessor struct {
	// contains filtered or unexported fields
}

StyleProcessor handles StyleStruct processing

func NewStyleProcessor added in v1.1.0

func NewStyleProcessor(imgProc ImageProcessorInterface) *StyleProcessor

NewStyleProcessor creates a new style processor

func (*StyleProcessor) ProcessStyleStruct added in v1.1.0

func (sp *StyleProcessor) ProcessStyleStruct(style *jplaw.StyleStruct) string

ProcessStyleStruct processes a StyleStruct and returns HTML

Directories

Path Synopsis
cmd
jplaw2epub command

Jump to

Keyboard shortcuts

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