Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Add e-mail lexer (rouge-ruby#1567)
Browse files Browse the repository at this point in the history
This commit adds a lexer for e-mails. It follows RFC 2822 as well as supporting
quoted lines in the form described in RFC 3676.

Co-authored-by: Michael Camilleri <mike@inqk.net>
  • Loading branch information
2 people authored and mattt committed May 19, 2021
1 parent 2dd87f2 commit 3a0e6e3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/rouge/demos/email
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
From: Me <me@example.com>
To: You <you@example.com>
Date: Tue, 21 Jul 2020 15:14:03 +0000
Subject: A very important message

> Please investigate. Thank you.

I have investigated.

--
This message is highly confidential and will self-destruct.
39 changes: 39 additions & 0 deletions lib/rouge/lexers/email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class Email < RegexLexer
tag 'email'
aliases 'eml', 'e-mail'
filenames '*.eml'
mimetypes 'message/rfc822'

title "Email"
desc "An email message"

start do
push :fields
end

state :fields do
rule %r/[:]/, Operator, :field_body
rule %r/[^\n\r:]+/, Name::Tag
rule %r/[\n\r]/, Name::Tag
end

state :field_body do
rule(/(\r?\n){2}/) { token Text; pop!(2) }
rule %r/\r?\n(?![ \v\t\f])/, Text, :pop!
rule %r/[^\n\r]+/, Name::Attribute
rule %r/[\n\r]/, Name::Attribute
end

state :root do
rule %r/\n/, Text
rule %r/^>.*/, Comment
rule %r/.*/, Text
end
end
end
end
18 changes: 18 additions & 0 deletions spec/lexers/email_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Email do
let(:subject) { Rouge::Lexers::Email.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.eml'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'message/rfc822'
end
end
end
23 changes: 23 additions & 0 deletions spec/visual/samples/email
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
From: Me <me@example.com>
To: You <you@example.com>
Cc: Somebody <somebody@example.com>,
And One More <andonemore@example.com>
Bcc: Secret Person <secretperson@email.com>
X-Spam-Status: Definitely not spam
Date: Tue, 21 Jul 2020 15:14:03 +0000
Subject: RE: A very important message
that continues onto the next line.

Greetings and salutations.

>> A second-level quotation.
>
> Please investigate. Thank you.

I have investigated.

Note: A space-stuffed line starting with > is not a quote.
> like this.

--
This message is highly confidential and will self-destruct.

0 comments on commit 3a0e6e3

Please sign in to comment.