This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |