百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

middleman-syntax

> 编程语言
开源

通过 Rouge 为 Middleman 添加代码语法高亮插件

180 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

通过 Rouge 为 Middleman 添加代码语法高亮插件

Middleman-Syntax

middleman-syntax is an extension for the Middleman static site generator that adds syntax highlighting via Rouge.

Installation

If you're just getting started, install the middleman gem and generate a new project:

bash
gem install middleman
middleman init MY_PROJECT

If you already have a Middleman project, add gem "middleman-syntax" to your Gemfile and run bundle install.

Configuration

ruby
activate :syntax

You can also pass options to Rouge:

ruby
activate :syntax, :line_numbers => true

You can add extra css classes to the pre tag elements:

ruby
activate :syntax, :extra_css_classes => ["custom-class", "another-class"]

This will add the extra css classes to the `` element in the generated HTML:

xml

The full set of options can be seen on your preview server's /__middleman/config/ page.

Helper

The extension adds a new code helper to Middleman that you can use from your templates. It will produce syntax-highlighted HTML wrapped in ...html....

In Erb, use ` def my_cool_method(message) puts message end


**NOTE:** In Haml, use `=`, not `-`:

```haml
= code('ruby') do
  puts "hello"

For more on Haml syntax, see the "Haml" section below.

In Slim:

slim
= code('ruby') do
  |
    puts 'hello'

The code helper supports Rouge instance formatter options. These override the defaults set in your config.rb. Example options include:

  • line_numbers
  • start_line
  • css_class
  • wrap
  • extra_css_classes

To use these formatter options per code block, include them in a hash as the second argument. e.g.

erb
 true, :start_line => 7) do %>
def my_cool_method(message)
  puts message
end

You can also add extra css classes to specific code blocks:

erb
 ["custom-class", "special"]) do %>
def my_cool_method(message)
  puts message
end

This will produce HTML like:

xml

CSS

On a default (i.e. unstyled) Middleman project, it will appear as if middleman-syntax isn't working, since obviously no CSS has been applied to color your code. You can use any Pygments-compatible stylesheet to style your code.

You can also let Rouge generate some CSS for you by creating a new stylesheet with a .css.erb extension in your Middleman project (at a path like source/stylesheets/highlighting.css.erb) with the contents:

erb
 '.highlight') %>

And then include it in your layout or specific page by:

erb

If you want to include this in a larger Sass stylesheet, include it in your main stylesheet with @import 'highlighting.css'.

Rouge has ThankfulEyes, Colorful, Github, Base16, Base16::Solarized, Base16::Monokai, and Monokai themes.

Markdown

The extension also makes code blocks in Markdown produce highlighted code. Make sure you're using Redcarpet or Kramdown as your Markdown engine (in config.rb):

ruby
set :markdown_engine, :redcarpet
set :markdown, :fenced_code_blocks => true, :smartypants => true

## OR

set :markdown_engine, :kramdown

Now your Markdown will work just like it does on GitHub - you can write something like this with Redcarpet:

ruby
def my_cool_method(message)
  puts message
end

You can also disable the line numbers on a specific code block. However, this is Middleman-syntax specific feature, which only works when using Redcarpet.

Disabling the line numbers on a code block:

def my_cool_method(message)
  puts message
end

or with Kramdown:

ruby
def my_cool_method(message)
  puts message
end

Haml

When using Haml, a :code filter is exposed for outputting highlighted code. Because Haml filters don't allow arguments, you must use a special comment to indicate the language of the code to be highlighted (or let Rouge guess):

haml
#example
  :code
    # lang: ruby

    def foo
      puts 'bar'
    end

…

haml
- code('ruby') do
  :preserve
    def foo
      puts 'bar'
    end

Community

The official community forum is available at: http://forum.middlemanapp.com

Bug Reports

Github Issues are used for managing bug reports and feature requests. If you run into issues, please search the issues and submit new problems: https://github.com/middleman/middleman-syntax/issues

The best way to get quick responses to your issues and swift fixes to your bugs is to submit detailed bug reports, include test cases and respond to developer questions in a timely manner. Even better, if you know Ruby, you can submit Pull Requests containing Cucumber Features which describe how your feature should work or exploit the bug you are submitting.

How to Run Cucumber Tests

  1. Checkout Repository: git clone https://github.com/middleman/middleman-syntax.git
  2. Install Bundler: gem install bundler
  3. Run bundle install inside the project root to install the gem dependencies.
  4. Run test cases: bundle exec rake test

Donate

Click here to lend your support to Middleman

License

Copyright (c) 2012-2014 Benjamin Hollis. MIT Licensed, see LICENSE for details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubycode-highlightmiddleman-extensionsyntax-highlighting

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言