jxxcarlson/scripta-compiler/2.5.1

NOTE: the latest version is 4.0.1

Scripta.API

Scripta.API provides the functions you will need for an application that compiles source text in L0, microLaTeX, or XMarkdown to HTML.

Simple compilation

compile : DisplaySettings -> Language -> String -> List (Element MarkupMsg)

Compile source text in the given language using the given display settings.

type alias DisplaySettings =
{ windowWidth : Int
, counter : Int
, selectedId : String
, selectedSlug : Maybe String
, scale : Float
}
  • windowWidth: set this to agree with the width of the window in pixels in which the rendered text is displayed.

  • counter: This is updated on each edit. For technical reasons (virtual Dom) this is needed for the text to display properly.

  • selectedId and selectedSlug: useful for interactive editing.

  • scale: a fudge factor

Example

compile (displaySettings 0) "Pythagorean formula: $a^2 + b^2 = c^2$" where we define

displaySettings : Int -> Scripta.API.DisplaySettings
displaySettings counter =
    { windowWidth = 500
    , counter = counter
    , selectedId = "--"
    , selectedSlug = Nothing
    , scale = 0.8
    }

The counter field must be updated on each edit. This is needed for the rendered text to be properly updated. See the demo app in folder Example1.

Differential Compilation

Compilation can be sped up by keeping track of which blocks of source text have changed and ony reparsing those blocks. An EditRecord is used to keep track of what has changed and what has not. In this setup, the EditRecord is initialized with the source text using the init function. On each document change it brought up to date by the update function. The render function transforms the current EditRecord into HTML.

type alias EditRecord =
EditRecord
init : Dict String String -> Language -> String -> EditRecord
update : EditRecord -> String -> EditRecord
render : DisplaySettings -> EditRecord -> List (Element MarkupMsg)
makeSettings : String -> Maybe String -> Float -> Int -> Settings
defaultSettings : Settings

Export

The export and fileNameForExport are functions used to transform source text in a given markup language to standard LaTeX. The transformed text can be used to produce a PDF file or a tar files that contains both the standare LaTeX source and a folder of images used in the documents. See the code in modules PDF and Main of Example2 for more details. The Elm app sends data to https://pdfServ.app, a small server (165 lines of Haskell code) where it is turned into a PDF file or tar archive where it is then accessible by a GET request. See pdfServer2@Github.

fileNameForExport : Forest ExpressionBlock -> String
packageNames : Forest ExpressionBlock -> List String
prepareContentForExport : Posix -> Settings -> Forest ExpressionBlock -> String
getImageUrls : Forest ExpressionBlock -> List String
type alias Settings =
{ paragraphSpacing : Int
, selectedId : String
, selectedSlug : Maybe String
, showErrorMessages : Bool
, showTOC : Bool
, titleSize : Int
, width : Int
, backgroundColor : Color
, titlePrefix : String
, isStandaloneDocument : Bool
}

Settings used by render

getBlockNames : Forest ExpressionBlock -> List String
rawExport : Settings -> Forest ExpressionBlock -> String
encodeForPDF : Posix -> Settings -> Forest ExpressionBlock -> Value

Compatibility

The PDF module in Example2 requires these.

type alias Msg =
type alias SyntaxTree =
Forest ExpressionBlock

Utility

matchingIdsInAST : String -> Forest ExpressionBlock -> List String