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 1 commit
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
Next Next commit
Add a lexer for emails
  • Loading branch information
smokris committed Jul 22, 2020
commit bd456920637bcaa518fe7c20673ce51137daaeb5
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.
46 changes: 46 additions & 0 deletions lib/rouge/lexers/email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

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

title "Email"
desc "An email message"

smokris marked this conversation as resolved.
Show resolved Hide resolved
def self.detect?(text)
return true if text.start_with?('From: ')
smokris marked this conversation as resolved.
Show resolved Hide resolved
end

state :root do
rule %r/^(From|To|Cc|Bcc):\s/, Keyword, :address
rule %r/^Date:\s/, Keyword, :date
rule %r/^Subject:\s/, Keyword, :subject
rule %r/\n/m, Text::Whitespace
rule %r/^>.*/, Comment
rule %r/^--\s\n/m, Comment::Doc, :signature
rule %r/.*/, Text
smokris marked this conversation as resolved.
Show resolved Hide resolved
end

state :address do
rule %r/[^\n]+\n/m, Name, :pop!
end

state :date do
rule %r/[^\n]+\n/m, Literal::Date, :pop!
end

state :subject do
rule %r/[^\n]+\n/m, Name::Label, :pop!
end

state :signature do
rule %r/.*/m, Comment::Doc
end
end
end
end
23 changes: 23 additions & 0 deletions spec/lexers/email_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- 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'
assert_guess :filename => 'foo.mail'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'message/rfc822'
end

it 'guesses by source' do
assert_guess :source => 'From: Me <me@example.com>'
end
end
end
11 changes: 11 additions & 0 deletions spec/visual/samples/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.