An open API service indexing awesome lists of open source software.

https://github.com/leandrocp/autumn

Syntax highlighter powered by Tree-sitter and Neovim themes.
https://github.com/leandrocp/autumn

elixir rust syntax-highlighter syntax-highlighting tree-sitter

Last synced: about 1 month ago
JSON representation

Syntax highlighter powered by Tree-sitter and Neovim themes.

Awesome Lists containing this project

README

        

# Autumn


Autumn logo


Syntax highlighter powered by Tree-sitter and Neovim themes.


https://autumnus.dev



Hex Version


Hex Docs


MIT

## Features

- 🌳 60+ languages with tree-sitter parsing
- 🎨 100+ Neovim themes
- 📝 HTML output with inline or linked styles
- 🖥️ Terminal output with ANSI colors
- 🔍 Language auto-detection
- 🎯 Customizable formatting options

## Installation

```elixir
def deps do
[
{:autumn, "~> 0.3"}
]
end
```

## Usage

### Basic Usage (HTML Inline)

```elixir
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir")
~s|

Atom.to_string(:elixir)

|
```

See the HTML Linked and Terminal formatters below for more options.

### Language Auto-detection

```elixir
iex> Autumn.highlight!("#!/usr/bin/env bash\nID=1")
~s|

#!/usr/bin/env bash

ID=1
|
```

### Themes

Themes can be specified either by name or by using a theme struct:

```elixir
# Using theme name
iex> Autumn.highlight!("setTimeout(fun, 5000);", language: "js", theme: "github_light")
~s|

setTimeout(fun, 5000);

|

# Using theme struct
iex> theme = Autumn.Theme.get("github_light")
iex> Autumn.highlight!("setTimeout(fun, 5000);", language: "js", theme: theme)
```

## Incomplete or Malformed code

It's also capable of handling incomplete or malformed code, useful for streaming like in a ChatGPT interface:

```elixir
iex> Autumn.highlight!("const header = document.getEl", language: "js")
~s|

const header = document.getEl

|
```

## Formatters

Autumn supports three output formatters:

Both HTML formatters wrap each line in a `` element with a `data-line` attribute containing the line number, making it easy to add line numbers or implement line-based features in your application.

### HTML Inline (Default)

Generates HTML with inline styles for each token:

```elixir
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir", formatter: :html_inline)
# or with options
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir", formatter: {:html_inline, pre_class: "my-code", italic: true, include_highlights: true})
```

Options:
- `:pre_class` - CSS class for the `

` tag

- `:italic` - enable italic styles
- `:include_highlights` - include highlight scope names in `data-highlight` attributes

### HTML Linked

Generates HTML with CSS classes for styling:

```elixir
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir", formatter: :html_linked)
# or with options
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir", formatter: {:html_linked, pre_class: "my-code"})
```

Options:
- `:pre_class` - CSS class for the `

` tag

To use linked styles, you need to include one of the [available CSS themes](https://github.com/leandrocp/autumn/tree/main/priv/static/css) in your app.

For Phoenix apps, add this to your `endpoint.ex`:

```elixir
plug Plug.Static,
at: "/themes",
from: {:autumn, "priv/static/css/"},
only: ["dracula.css"] # choose any theme you want
```

Then add the stylesheet to your template:

```html

```

### Terminal

Generates ANSI escape codes for terminal output:

```elixir
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir", formatter: :terminal)
# or with options
iex> Autumn.highlight!("Atom.to_string(:elixir)", language: "elixir", formatter: {:terminal, italic: true})
```

Options:
- `:italic` - enable italic styles (if supported by your terminal)

## Themes

Use `Autumn.available_themes/0` to list all available themes and `Autumn.Theme.get/1` to get a specific theme. Themes are sourced from popular Neovim colorschemes.

## Samples

Visit https://autumnus.dev to check out some examples.

## Looking for help with your Elixir project?

DockYard logo

At DockYard we are [ready to help you build your next Elixir project](https://dockyard.com/phoenix-consulting).
We have a unique expertise in Elixir and Phoenix development that is unmatched and we love to [write about Elixir](https://dockyard.com/blog/categories/elixir).

Have a project in mind? [Get in touch](https://dockyard.com/contact/hire-us)!

## Acknowledgements

* [Logo](https://www.flaticon.com/free-icons/fall) created by by pongsakornRed - Flaticon
* [Logo font](https://fonts.google.com/specimen/Sacramento) designed by [Astigmatic](http://www.astigmatic.com)
* [Makeup](https://hex.pm/packages/makeup) for setting up the baseline and for the inspiration
* [Inkjet](https://crates.io/crates/inkjet) for the Rust implementation up to v0.2 and for the inspiration