ernest

Convert Gleam diagnostics to colourful HTML, ansi code, or any other format you want.

pub fn diagnostic_to_html(content: String) -> String {
  case ernest.parse(content) {
    // The diagnostic parsed successfully, convert it to colourful HTML
    Ok(diagnostic) ->
      "<pre><code>" <> ernest.to_html(diagnostic) <> "</pre></code>"

    // This doesn't seem to be a diagnostic, pass it through as-is.
    Error(Nil) -> "<pre><code>" <> content <> "</pre></code>"
  }
}

Types

pub type CodeLine {
  CodeLine(
    gutter: String,
    before: String,
    highlight: String,
    after: String,
  )
}

Constructors

  • CodeLine(
      gutter: String,
      before: String,
      highlight: String,
      after: String,
    )
pub type Diagnostic {
  Diagnostic(
    level: Level,
    title: String,
    path: String,
    line: Int,
    column: Int,
    gutter_size: Int,
    code: List(CodeLine),
    text: String,
  )
}

Constructors

  • Diagnostic(
      level: Level,
      title: String,
      path: String,
      line: Int,
      column: Int,
      gutter_size: Int,
      code: List(CodeLine),
      text: String,
    )
pub type Level {
  ErrorLevel
  WarningLevel
}

Constructors

  • ErrorLevel
  • WarningLevel
pub type Token {
  Plain(String)
  PlainBold(String)
  Cyan(String)
  Red(String)
  Yellow(String)
  RedBold(String)
  YellowBold(String)
}

Constructors

  • Plain(String)
  • PlainBold(String)
  • Cyan(String)
  • Red(String)
  • Yellow(String)
  • RedBold(String)
  • YellowBold(String)

Values

pub fn parse(diagnostic: String) -> Result(Diagnostic, Nil)

Parse a Gleam diagnostic from a string that has been printed to the terminal.

pub fn to_ansi(diagnostic: Diagnostic) -> String

Highlight a Gleam diagnostic using terminal ansi colours!

If you wish to use some other colours or other format entirely see to_tokens.

pub fn to_html(diagnostic: Diagnostic) -> String

Convert a parsed Gleam diagnostic into a HTML string! Each token is wrapped in a <span> with a class indicating the type of token.

The class names used are:

  • diagnostic-bold
  • diagnostic-cyan
  • diagnostic-red
  • diagnostic-yellow
  • diagnostic-red-bold
  • diagnostic-yellow-bold

Place the output within a <pre><code>...</code></pre> and add styling for these CSS classes to get highlightin on your website. Here’s some CSS you could use:

pre code .diagnostic-bold        { font-weight: bold }
pre code .diagnostic-red-bold    { font-weight: bold; color: #ff6262 }
pre code .diagnostic-yellow-bold { font-weight: bold; color: #ffd596 }
pre code .diagnostic-cyan        { color: #9ce7ff }
pre code .diagnostic-red         { color: #ff6262 }
pre code .diagnostic-yellow      { color: #ffd596 }

If you wish to use some other format see to_tokens.

pub fn to_tokens(diagnostic: Diagnostic) -> List(Token)

Convert a Gleam diagnostic code to a list of tokens, which you can then convert into whatever format you want.

If you wish to print to the terminal using ansi colours see to_ansi.

✨ Search Document