Skip to content

Commit

Permalink
✨ Add the ability to identify em dashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
myles committed Aug 16, 2018
1 parent dd8bf0f commit 95d9d67
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -138,6 +138,16 @@ Let's make sure click doesn't look like dick.
<p>There’s more to love with every <span class="fix-letter-spacing">click</p>.</p>
```

### Em dash

Identify em dashes and surround them with a span.

```html
<p>{{ "Upon discovering the errors—all 124 of them—the publisher immediately recalled the books." | jt_emdash }}</p>

<p>Upon discovering the errors<span class="emdash">&mdash;</span>all 124 of them<span class="emdash">&mdash;</span>the publisher immediately recalled the books.</p>
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
20 changes: 20 additions & 0 deletions lib/jekyll/typogrify.rb
Expand Up @@ -103,6 +103,15 @@ def jt_caps(text)
return custom_caps(text.to_s)
end

# converts a — (em dash) by optional whitespace or a non-breaking space
# to the HTML entity and surrounds it in a span with a styled class.
#
# @param [String] text input text
# @return [String] input text with em dashes wrapped
def jt_emdash(text)
return emdash(text.to_s)
end

private

# custom modules to jekyll-typogrify
Expand Down Expand Up @@ -133,6 +142,17 @@ def custom_caps(text)
end
end
end

# converts a — (em dash) by optional whitespace or a non-breaking space
# to the HTML entity and surrounds it in a span with a styled class.
#
# @param [String] text input text
# @return [String] input text with em dashes wrapped
def emdash(text)
text.gsub(/(\s|&nbsp;)—(?:mdash;|#8212;)?(\s|&nbsp;)/) { |str|
$1 + '<span class="emdash">&mdash;</span>' + $2
}.gsub(/(\w+)="(.*?)<span class="emdash">&mdash;<\/span>(.*?)"/, '\1="\2&mdash;\3"')
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/_layouts/default.html
Expand Up @@ -38,5 +38,7 @@
<p class="test-hyphenated-caps">{{ "The M65-A aircraft weights 10 tons." | caps }}</p>

<p class="test-hyphenated-jt_caps">{{ "The M65-A aircraft weights 10 tons." | jt_caps }}</p>

<p class="test-jt_emdash">{{ "And yet, when the car was finally delivered—nearly three months after it was ordered—she decided she no longer wanted it, leaving the dealer with an oddly equipped car that would be difficult to sell." | jt_emdash }}</p>
</body>
</html>
5 changes: 5 additions & 0 deletions spec/jekyll/typogrify_spec.rb
Expand Up @@ -114,4 +114,9 @@
expect(contents).to match /<p class="test-hyphenated-jt_caps">The <span class="caps">M65-A<\/span> aircraft weights 10 tons.<\/p>/
expect(contents).to_not match /<p class="test-hyphenated-jt_caps">The <span class="caps">M65<\/span>-A aircraft weights 10 tons.<\/p>/
end

it "test jt_emdash" do
expect(contents).to match /<p class="test-jt_emdash">And yet, when the car was finally delivered—nearly three months after it was ordered—she decided she no longer wanted it, leaving the dealer with an oddly equipped car that would be difficult to sell.<\/p>/
expect(contents).to_not match /<p class="test-jt_emdash">And yet, when the car was finally delivered<span class="emdash">&mdash;<\/span>nearly three months after it was ordered<span class="emdash">&mdash;<\/span>she decided she no longer wanted it, leaving the dealer with an oddly equipped car that would be difficult to sell.<\/p>/
end
end

0 comments on commit 95d9d67

Please sign in to comment.