Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a lexer for emails #1567

Merged
merged 7 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
smokris marked this conversation as resolved.
Show resolved Hide resolved
mimetypes 'message/rfc822'

title "Email"
desc "An email message"

smokris marked this conversation as resolved.
Show resolved Hide resolved
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) }
smokris marked this conversation as resolved.
Show resolved Hide resolved
rule %r/\r?\n(?![ \v\t\f])/, Text, :pop!
smokris marked this conversation as resolved.
Show resolved Hide resolved
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.