rtypeset 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8ea4e5bc767657e85cce15701a16bd5d7509758
4
+ data.tar.gz: a5fb8f89579eeb8343354e42a75ef533e77a7c2d
5
+ SHA512:
6
+ metadata.gz: d6c17fb385f25ec901ba85fa1f713f34ccf15ca6b9ce366d6a89355f4e2f1932984c18a8a49214f142582c77c8b94f33b16aca6430e332ca61de892acc01b81b
7
+ data.tar.gz: eee4219f33268dc297a300ed3040eb185ad463376b97be164f9e510d8bbb122b2d4e567a2e800da2192edc1ed1b9d49bf7ea89d959c04d0e913ad84faaccfd05
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.1.0
5
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "text-hyphen", "~> 1.4"
6
+ gem "nokogiri", ">= 1.6"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "minitest", ">= 5.8.0"
12
+ gem "shoulda", ">= 0"
13
+ gem "rdoc", "~> 3.12"
14
+ gem "bundler", "~> 1.0"
15
+ gem "jeweler", "~> 2.0.1"
16
+ gem "simplecov", ">= 0"
17
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Trevor Fountain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # An HTML pre-processor for web typography
2
+
3
+ [![Build Status](https://travis-ci.org/doches/rtypeset.png)](https://travis-ci.org/doches/rtypeset)
4
+ [![Documentation Status](https://inch-ci.org/github/doches/rtypeset.svg?branch=master)](https://inch-ci.org/github/doches/rtypeset)
5
+
6
+ a pure Ruby typographic pre-processor for HTML inspired by [Typeset.js](https://github.com/davidmerfield/Typeset) that gives you:
7
+
8
+ * Real hang­ing punc­tu­a­tion
9
+ * Soft hy­phen in­ser­tion
10
+ * Op­ti­cal mar­gin out­dents
11
+ * Small-caps conversion
12
+ * Punc­tu­a­tion sub­sti­tu­tion
13
+ * Space sub­sti­tu­tion
14
+
15
+ ## Installation
16
+
17
+ gem install rtypeset
18
+
19
+ ## Usage
20
+
21
+ Just require the `typeset` gem and then call `#typeset` to receive beautifully-formatted
22
+ HTML:
23
+
24
+ require 'typeset'
25
+
26
+ raw_html = <<HTM
27
+ <p>Yjarni Sigurðardóttir spoke to NATO from Iceland yesterday:
28
+ "Light of my life, fire of my florins -- my sin, my soul.
29
+ The tip of the tongue taking a trip to 118° 19' 43.5".":</p>
30
+ HTM
31
+
32
+ # Output beautifully-formatted HTML
33
+ puts Typeset.typeset(raw_html)
34
+
35
+ Want more control over your typesetting? You can selectively disable Typeset features by passing
36
+ in an options hash to `#typeset`:
37
+
38
+ # Disable hyphenation and small caps conversion.
39
+ options = {:disable => [:hyphenate, :small-caps]}
40
+ Typeset.typeset(raw_html, options)
41
+
42
+ The full list of disablable modules is: `:quotes`, `:hanging_punctuation`, `:spaces`, `:small_caps`, `:hyphenate`, `:ligatures` and `:punctuation`.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "rtypeset"
18
+ gem.homepage = "http://github.com/doches/rtypeset"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{An HTML pre-processor for web typography}
21
+ gem.description = %Q{A pure Ruby implementation of Typeset.js, an HTML pre-processor for web typography}
22
+ gem.email = "trevor@texasexpat.net"
23
+ gem.authors = ["Trevor Fountain"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ desc "Code coverage detail"
36
+ task :simplecov do
37
+ ENV['COVERAGE'] = "true"
38
+ Rake::Task['test'].execute
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "rtypeset #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/demo/demo.rb ADDED
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'typeset'
4
+
5
+ puts Typeset.typeset(STDIN.readlines().join(""))
data/demo/raw.html ADDED
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <head>
3
+ <meta charset="utf-8">
4
+ <meta name="viewport" content="width=device-width, initial-scale=1">
5
+ <title>Typeset.js – an HTML pre-processor for web typography</title>
6
+ <link rel="icon" type="image/png" href="./demo/favicon.png">
7
+ <link href='http://fonts.googleapis.com/css?family=Slabo+27px' rel='stylesheet' type='text/css'>
8
+ <link rel="stylesheet" href="./style.css">
9
+ <link rel="stylesheet" href="./typeset.css">
10
+ </head>
11
+ <body>
12
+ <div class="demo">
13
+ <p>Yjarni Sigurðardóttir spoke to NATO from Iceland yesterday: "Light of my life, fire of my florins -- my sin, my soul. The tip of the tongue taking a trip to 118° 19' 43.5"."</p>
14
+
15
+ <p>"She's faster than a 120' 4" whale." <em>Piña co­ladas</em> were widely consumed in Götterdämmerung from 1880–1912. For the low price of $20 / year from Ex­hi­bits A–E... Then the <em>duplex</em> came forward. "Thrice the tower, he mounted the round gunrest, 'awaking' HTML. He can print a fixed num­ber of dots in a square inch (for in­stance, 600 × 600)."</p>
16
+ </div>
17
+ </body>
18
+ </html>
data/demo/style.css ADDED
@@ -0,0 +1,61 @@
1
+ @import url(http://fonts.googleapis.com/css?family=Alegreya+SC);
2
+ @import url(http://fonts.googleapis.com/css?family=Alegreya);
3
+ @import url(http://fonts.googleapis.com/css?family=Inconsolata);
4
+
5
+ body {
6
+ font-family: 'Alegreya', serif;
7
+ }
8
+
9
+ p {
10
+ font-size: x-large;
11
+ }
12
+
13
+ .demo {
14
+ border-left: 4px solid #ddd;
15
+ border-radius: 2px;
16
+ padding-left: 8px;
17
+ }
18
+
19
+ .container {
20
+ width: 600px;
21
+ margin: 3em auto;
22
+ }
23
+
24
+ li {
25
+ list-style-type: none;
26
+ display: block;
27
+ width: 40%;
28
+ float: left;
29
+ margin-right: 1em;
30
+ margin-bottom: 0.33em;
31
+ }
32
+
33
+ .prefooter {
34
+ clear: both;
35
+ height: 1px;
36
+ display: block;
37
+ }
38
+
39
+ h2,h3,h4,h5 {
40
+ font-family: 'Alegreya SC', serif;
41
+ text-transform: lowercase;
42
+ color: #222;
43
+ width: 100%;
44
+ border-bottom-color: #ddd;
45
+ border-bottom-width: 1px;
46
+ border-bottom-style: solid;
47
+ }
48
+
49
+ h3 {
50
+ border-bottom-color: #F0aCaC;
51
+ color: #F03C3C;
52
+ }
53
+
54
+ .code {
55
+ font-family: 'Inconsolata', monospace;
56
+ }
57
+
58
+ a {
59
+ text-decoration: none;
60
+ color: #F03C3C;
61
+ }
data/demo/typeset.css ADDED
@@ -0,0 +1,23 @@
1
+ /* Small caps */
2
+ .small-caps {
3
+ font-family: 'Alegreya SC', sans-serif;
4
+ text-transform: lowercase;
5
+ }
6
+
7
+ /* Double quote (") marks */
8
+ .pull-double{margin-left:-.38em}
9
+ .push-double{margin-right:.38em}
10
+
11
+ /* Single quote (') marks */
12
+ .pull-single{margin-left:-.15em}
13
+ .push-single{margin-right:.15em}
14
+
15
+ /* Optical margin alignment for particular letters */
16
+ .pull-T, .pull-V, .pull-W, .pull-Y {margin-left: -0.07em}
17
+ .push-T, .push-V, .push-W, .push-Y {margin-right: 0.07em}
18
+
19
+ .pull-O, .pull-C, .pull-o, .pull-c {margin-left: -0.04em}
20
+ .push-O, .push-C, .push-o, .push-c {margin-right: 0.04em}
21
+
22
+ .pull-A {margin-left: -0.03em}
23
+ .push-A {margin-right: 0.03em}
data/index.html ADDED
@@ -0,0 +1,52 @@
1
+ <!DOCTYPE html>
2
+ <head>
3
+ <meta charset="utf-8">
4
+ <meta name="viewport" content="width=device-width, initial-scale=1">
5
+ <title> <span class="pull-T">T</span>ype­set.js – an <span class="small-caps">HTML</span> pre-proces­sor ­for<span class="push-w"></span> <span class="pull-w">w</span>eb ­ty­pog­ra­phy<span class="push-T"></span> </title>
6
+ <link rel="icon" type="image/png" href="./demo/favicon.png">
7
+ <link rel="stylesheet" href="./demo/style.css">
8
+ <link rel="stylesheet" href="./demo/typeset.css">
9
+ </head>
10
+ <body>
11
+ <div class="container">
12
+ <h1>rTypeset</h1>
13
+ <p>A typographic pre-processor for <span class='small-caps'>HTML</span> inspired by <a href="https://blot.im/typeset/">Typeset.js</a> that gives you:</p>
14
+ <ul>
15
+ <li>Real hang­ing punc­tu­a­tion</li>
16
+ <li>Soft hy­phen in­ser­tion</li>
17
+ <li><span class="pull-O">O</span>p­ti­cal mar­gin<span class="push-o"></span> <span class="pull-o">o</span>ut­dents</li>
18
+ <li>Small-caps<span class="push-c"></span> <span class="pull-c">c</span>on­ver­sion</li>
19
+ <li>Punc­tu­a­tion sub­sti­tu­tion</li>
20
+ <li>Space sub­sti­tu­tion</li>
21
+ </ul>
22
+ <div class='prefooter'>&nbsp;</div>
23
+ <h3>Raw</h3>
24
+ <div class="demo">
25
+ <p>Yjarni Sigurðardóttir spoke to NATO from Iceland yesterday: "Light of my life, fire of my florins -- my sin, my soul. The tip of the tongue taking a trip to 118° 19' 43.5"."</p>
26
+ <p>"She's faster than a 120' 4" whale." <em>Piña co­ladas</em> were widely consumed in Götterdämmerung from 1880–1912. For the low price of $20 / year from Ex­hi­bits A–E... Then the <em>duplex</em> came forward. "Thrice the tower, he mounted the round gunrest, 'awaking' HTML. He can print a fixed num­ber of dots in a square inch (for in­stance, 600 × 600)."</p>
27
+ </div>
28
+ <h3>Typeset</h3>
29
+ <div class="demo">
30
+ <p><span class="pull-Y">Y</span>jarni Sig­urðardót­tir spoke to <span class="small-caps">NATO</span> from Ice­land yes­ter­day:<span class="push-double"></span> <span class="pull-double">“</span>Light<span class="push-o"></span> <span class="pull-o">o</span>f my life, ­fire<span class="push-o"></span> <span class="pull-o">o</span>f my florin­s – my s­in, my ­soul.<span class="push-T"></span> <span class="pull-T">T</span>he tip<span class="push-o"></span> <span class="pull-o">o</span>f the ­tongue ­tak­ing a trip ­to 118° 19′ 43.5″.”<span class="push-Y"></span><span class="push-Y"></span></p>
31
+ <p><span class="pull-double">“</span>She’s ­faster than a 120′ 4″<span class="push-w"></span> <span class="pull-w">w</span>hale.”<span class="push-double"></span> <em>Piña<span class="push-c"></span> <span class="pull-c">c</span>o­ladas</em> <span class="pull-w">w</span>ere<span class="push-w"></span> <span class="pull-w">w</span>idely<span class="push-c"></span> <span class="pull-c">c</span>onsumed in Göt­ter­däm­merung from 1880–1912. ­For the low price<span class="push-o"></span> <span class="pull-o">o</span>f $20 / year from Ex­hi­bits<span class="push-A"></span> <span class="pull-A">A</span>–E…<span class="push-T"></span> <span class="pull-T">T</span>hen the<span class="push-w"></span> <em>duplex</em> <span class="pull-c">c</span>ame ­for­ward.<span class="push-double"></span> <span class="pull-double">“</span>Thrice the ­tow­er, he ­mount­ed the round ­gun­rest,<span class="push-single"></span> <span class="pull-single">‘</span>awak­ing’ <span class="small-caps">HTML</span>. He<span class="push-c"></span> <span class="pull-c">c</span>an print­ a ­fixed num­ber<span class="push-o"></span> <span class="pull-o">o</span>f dot­s in a square inch (­for in­s­tance, 600 × 600).”<span class="push-c"></span></p>
32
+ </div>
33
+ <h2>Installation</h2>
34
+ <code class='code'>
35
+ gem install rtypeset
36
+ </code>
37
+ <h2>Usage</h2>
38
+ <pre class='code'>
39
+ require 'typeset'
40
+
41
+ raw_html = &lt;&lt;HTM
42
+ &lt;p&gt;Yjarni Sigurðardóttir spoke to NATO from Iceland yesterday:
43
+ "Light of my life, fire of my florins -- my sin, my soul.
44
+ The tip of the tongue taking a trip to 118° 19' 43.5".":&lt;/p&gt;
45
+ HTM
46
+
47
+ # Output beautifully-formatted HTML
48
+ puts Typeset.typeset(raw_html)
49
+ </pre>
50
+ </div>
51
+ </body>
52
+ </html>
data/lib/typeset.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'typeset/quotes'
2
+ require 'typeset/hyphenate'
3
+ require 'typeset/ligatures'
4
+ require 'typeset/small_caps'
5
+ require 'typeset/punctuation'
6
+ require 'typeset/hanging_punctuation'
7
+ require 'typeset/spaces'
8
+ require 'nokogiri'
9
+
10
+ # Contains all of our typeset-related class methods. Mix this module into a
11
+ # class, or just call `Typeset#typset` directly
12
+ module Typeset
13
+ # Parse an HTML fragment with Nokogiri and apply a function to all of the
14
+ # descendant text nodes
15
+ def self.apply_to_text_nodes(html, &func)
16
+ doc = Nokogiri::HTML("<div id='rtypeset_internal'>#{html}</div>", nil,"UTF-8",Nokogiri::XML::ParseOptions::NOENT)
17
+ doc.search('//text()').each do |node|
18
+ old_content = node.content
19
+ new_content = func.call(node.content.strip)
20
+ if old_content =~ /^(\s+)/
21
+ new_content = " #{new_content}"
22
+ end
23
+ if old_content =~ /(\s+)$/
24
+ new_content = "#{new_content} "
25
+ end
26
+ node.replace(new_content)
27
+ end
28
+ content = doc.css("#rtypeset_internal")[0].children.map { |child| child.to_html }
29
+ return content.join("")
30
+ end
31
+
32
+ # The main entry point for Typeset. Pass in raw HTML or text, along with an
33
+ # optional options block.
34
+ def self.typeset(html, options={})
35
+ methods = [
36
+ [:quotes, true],
37
+ [:hanging_punctuation, true],
38
+ [:spaces, true],
39
+ [:small_caps, true],
40
+ [:hyphenate, true],
41
+ [:ligatures, false],
42
+ [:punctuation, false]
43
+ ]
44
+
45
+ options[:disable] ||= []
46
+ methods.reject! { |method| options[:disable].include?(method[0]) }
47
+
48
+ methods.each do |func, use_text_nodes|
49
+ new_html = html
50
+ if use_text_nodes
51
+ new_html = Typeset.apply_to_text_nodes(html) { |content| Typeset.send(func, content, options) }
52
+ else
53
+ new_html = Typeset.send(func, html, options).strip
54
+ end
55
+ html = new_html
56
+ end
57
+ return html
58
+ end
59
+ end
@@ -0,0 +1,64 @@
1
+ module Typeset
2
+ # Contains constants and methods specific to our hanging punctuation implementation
3
+ module HangingPunctuation
4
+ # Characters that need a double-width pull
5
+ DoubleWidth = ['&quot;', '"', "“", "„", "”", "&ldquo;", "&OpenCurlyDoubleQuote;", "&#8220;", "&#x0201C;", "&rdquor;", "&rdquo;", '&CloseCurlyDoubleQuote;', '&#8221;', '&ldquor;', '&bdquo;', '&#8222;']
6
+ # Characters that need a single-width pull
7
+ SingleWidth = ["'", '&prime;', '&apos;', '&lsquo;', '&rsquo;', '‘', '’']
8
+
9
+ # Wrap a piece of content in a pull class
10
+ def self.pull(classname, content='')
11
+ HangingPunctuation.wrap("pull", classname, content)
12
+ end
13
+
14
+ # Wrap a piece of content in a push class
15
+ def self.push(classname, content='')
16
+ HangingPunctuation.wrap("push", classname, content)
17
+ end
18
+
19
+ # Wrap a piece of content in an arbitrary class. Convenience method for
20
+ # implementing #push and #pull
21
+ def self.wrap(type, classname, content='')
22
+ "<span class=\"#{type}-#{classname}\">#{content}</span>"
23
+ end
24
+ end
25
+
26
+ # Add push/pull spans for hanging punctuation to text.
27
+ def self.hanging_punctuation(text, options)
28
+ return text if text.length < 2
29
+
30
+ aligns = "CcOoYTAVvWwY".split('')
31
+ words = text.split(/\s+/)
32
+ words.each_with_index do |word, i|
33
+ aligns.each do |align|
34
+ if word[0] == align
35
+ words[i] = "#{HangingPunctuation.pull(align, align)}#{word.slice(1,word.length)}"
36
+
37
+ if not words[i-1].nil?
38
+ words[i-1] = "#{words[i-1]}#{HangingPunctuation.push(align)}"
39
+ end
40
+ end
41
+ end
42
+ HangingPunctuation::SingleWidth.each do |punctuation|
43
+ if word[0] == punctuation
44
+ words[i] = "#{HangingPunctuation.pull('single', punctuation)}#{word.slice(1,word.length)}"
45
+
46
+ if not words[i-1].nil?
47
+ words[i-1] = "#{words[i-1]}#{HangingPunctuation.push('single')}"
48
+ end
49
+ end
50
+ end
51
+ HangingPunctuation::DoubleWidth.each do |punctuation|
52
+ if word[0] == punctuation
53
+ words[i] = "#{HangingPunctuation.pull('double', punctuation)}#{word.slice(1,word.length)}"
54
+
55
+ if not words[i-1].nil?
56
+ words[i-1] = "#{words[i-1]}#{HangingPunctuation.push('double')}"
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ return words.join(" ")
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ require 'text/hyphen'
2
+
3
+ module Typeset
4
+ # Hyphenate text, inserting soft hyphenation markers. Specify the language
5
+ # for hyphenation by passing in an options block to your typeset call, e.g.:
6
+ #
7
+ # Typeset.typeset("do hyphenation on this", {:language => "en_gb"})
8
+ def self.hyphenate(text, options)
9
+ options[:language] ||= 'en_us'
10
+ hyphen = Text::Hyphen.new(:language => options[:language])
11
+
12
+ text = hyphen.visualise(text, "\u00AD")
13
+
14
+ return text
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module Typeset
2
+ # Map of raw text sequences to unicode ligatures
3
+ Ligatures = {
4
+ 'ffi' => 'ffi',
5
+ 'ffl' => 'ffl',
6
+ 'fi' => 'fi',
7
+ 'fl' => 'fl',
8
+ 'st' => 'st',
9
+ 'ff' => 'ff',
10
+ 'ue' => 'ᵫ'
11
+ }
12
+
13
+ # List of ligatures to process by default
14
+ DefaultLigatures = %w{ffi ffl fi fl ff}
15
+
16
+ # Find and replace sequences of text with their unicode ligature equivalents.
17
+ # Override the set of ligatures to find by passing in a custom options hash, e.g.:
18
+ #
19
+ # Typeset.typeset("flue", {:ligatures => ["fl", "ue"]})
20
+ # # -> returns "flᵫ"
21
+ def self.ligatures(text, options)
22
+ options[:ligatures] ||= DefaultLigatures
23
+
24
+ options[:ligatures].each do |ligature|
25
+ text.gsub!(ligature, Ligatures[ligature])
26
+ end
27
+
28
+ return text
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ module Typeset
2
+ # Make dashes, elipses, and start/end punctuation a little prettier.
3
+ def self.punctuation(text, options)
4
+ # Dashes
5
+ text.gsub!('--', '–')
6
+ text.gsub!(' – ', "\u2009–\u2009")
7
+
8
+ # Elipses
9
+ text.gsub!('...', '…')
10
+
11
+ # Non-breaking space for start/end punctuation with spaces.
12
+ start_punc = /([«¿¡\[\(]) /
13
+ if text =~ start_punc
14
+ text.gsub!(start_punc, "#{$1}&nbsp;")
15
+ end
16
+ end_punc = / ([\!\?:;\.,‽»\]\)])/
17
+ if text =~ end_punc
18
+ text.gsub!(end_punc,"&nbsp;#{$1}")
19
+ end
20
+
21
+ return text
22
+ end
23
+ end
@@ -0,0 +1,45 @@
1
+ module Typeset
2
+ # A poor-man's Smarty Pants implementation. Converts single & double quotes,
3
+ # tick marks, backticks, and primes into prettier unicode equivalents.
4
+ def self.quotes(text, options)
5
+ # Unencode encoded characters, so our regex mess below works
6
+ text.gsub!('&#39;',"\'")
7
+ text.gsub!('&quot;',"\"")
8
+
9
+ if text =~ /(\W|^)"(\S+)/
10
+ text.gsub!(/(\W|^)"(\S+)/, "#{$1}\u201c#{$2}") # beginning "
11
+ end
12
+ if text =~ /(\u201c[^"]*)"([^"]*$|[^\u201c"]*\u201c)/
13
+ text.gsub!(/(\u201c[^"]*)"([^"]*$|[^\u201c"]*\u201c)/, "#{$1}\u201d#{$2}") # ending "
14
+ end
15
+ if text =~ /([^0-9])"/
16
+ text.gsub!(/([^0-9])"/, "#{$1}\u201d") # remaining " at end of word
17
+ end
18
+ if text =~ /(\W|^)'(\S)/
19
+ text.gsub!(/(\W|^)'(\S)/, "#{$1}\u2018#{$2}") # beginning '
20
+ end
21
+ if text =~ /([a-z])'([a-z])/i
22
+ text.gsub!(/([a-z])'([a-z])/i, "#{$1}\u2019#{$2}") # conjunction's possession
23
+ end
24
+ if text =~ /((\u2018[^']*)|[a-z])'([^0-9]|$)/i
25
+ text.gsub!(/((\u2018[^']*)|[a-z])'([^0-9]|$)/i, "#{$1}\u2019#{$3}") # ending '
26
+ end
27
+ if text =~ /(\u2018)([0-9]{2}[^\u2019]*)(\u2018([^0-9]|$)|$|\u2019[a-z])/i
28
+ text.gsub!(/(\u2018)([0-9]{2}[^\u2019]*)(\u2018([^0-9]|$)|$|\u2019[a-z])/i, "\u2019#{$2}#{$3}") # abbrev. years like '93
29
+ end
30
+ if text =~ /(\B|^)\u2018(?=([^\u2019]*\u2019\b)*([^\u2019\u2018]*\W[\u2019\u2018]\b|[^\u2019\u2018]*$))/i
31
+ text.gsub!(/(\B|^)\u2018(?=([^\u2019]*\u2019\b)*([^\u2019\u2018]*\W[\u2019\u2018]\b|[^\u2019\u2018]*$))/i, "#{$1}\u2019") # backwards apostrophe
32
+ end
33
+ text.gsub!(/'''/, "\u2034") # triple prime
34
+ text.gsub!(/("|'')/, "\u2033") # double prime
35
+ text.gsub!(/'/, "\u2032")
36
+
37
+ # Allow escaped quotes
38
+ text.gsub!('\\\“','\"')
39
+ text.gsub!('\\\”','\"')
40
+ text.gsub!('\\\’','\'')
41
+ text.gsub!('\\\‘','\'')
42
+
43
+ return text
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module Typeset
2
+ # Identify likely acronyms, and wrap them in a 'small-caps' span.
3
+ def self.small_caps(text, options)
4
+ words = text.split(" ")
5
+ words.each_with_index do |word, i|
6
+ if word =~ /^\W*([[:upper:]][[:upper:]][[:upper:]]+)\W*/
7
+ leading,trailing = word.split($1)
8
+ words[i] = "#{leading}<span class=\"small-caps\">#{$1}</span>#{trailing}"
9
+ end
10
+ end
11
+ return words.map { |x| x.strip }.join(" ")
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Typeset
2
+ # Replace wide (normal) spaces around math operators with hair spaces.
3
+ def self.spaces(text, options)
4
+ text.gsub!(" / ", "\u2009/\u2009")
5
+ text.gsub!(" × ", "\u2009×\u2009")
6
+ text.gsub!(" % ", "\u2009%\u2009")
7
+ text.gsub!(" + ", "\u2009+\u2009")
8
+
9
+ return text
10
+ end
11
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_profile 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ require 'rubygems'
18
+ require 'bundler'
19
+ begin
20
+ Bundler.setup(:default, :development)
21
+ rescue Bundler::BundlerError => e
22
+ $stderr.puts e.message
23
+ $stderr.puts "Run `bundle install` to install missing gems"
24
+ exit e.status_code
25
+ end
26
+ require 'minitest'
27
+ require 'minitest/autorun'
28
+ require 'shoulda'
29
+
30
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
31
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
32
+ require 'typeset'
33
+
34
+ class Minitest::Test
35
+ end
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+
3
+ class TestTypeset < Minitest::Test
4
+ Lorem = "Lorem ipsum dolor, sit <em class=\"latin\">amet</em>"
5
+ OptNoHyphenate = {:disable => [:hyphenate]}
6
+
7
+ def setup
8
+ assert_equal Lorem, Typeset.typeset(Lorem, OptNoHyphenate)
9
+ end
10
+
11
+ def test_quotes
12
+ assert_equal "“foo”", Typeset.typeset("\"foo\"", {:disable => [:hanging_punctuation]})
13
+ assert_equal "19′ 43.5″", Typeset.typeset("19\' 43.5\"")
14
+ end
15
+
16
+ def test_spaces
17
+ assert_equal "1\u2009/\u20092", Typeset.typeset("1 / 2")
18
+ assert_equal "1\u2009×\u20092", Typeset.typeset("1 × 2")
19
+ end
20
+
21
+ def test_punctuation
22
+ assert_equal "…", Typeset.typeset("...")
23
+ assert_equal "foo–bar", Typeset.typeset("foo--bar")
24
+ assert_equal "foo\u2009–\u2009bar", Typeset.typeset("foo -- bar")
25
+
26
+ assert_equal "¿&nbsp;foo&nbsp;?", Typeset.typeset("¿ foo ?", OptNoHyphenate)
27
+ assert_equal "bar [&nbsp;foo&nbsp;] baz", Typeset.typeset("bar [ foo ] baz", OptNoHyphenate)
28
+ end
29
+
30
+ def test_small_caps
31
+ assert_equal "an <span class=\"small-caps\">FBI</span> agent", Typeset.typeset("an FBI agent")
32
+ assert_equal "The <span class=\"small-caps\">CIA</span>", Typeset.typeset("The CIA", {:disable => [:hanging_punctuation, :hyphenate]})
33
+ assert_equal "<span class=\"small-caps\">NCSA</span>", Typeset.typeset("NCSA", OptNoHyphenate)
34
+ assert_equal "<span class=\"small-caps\">NCSA</span> Mosaic", Typeset.typeset("NCSA Mosaic", OptNoHyphenate)
35
+ assert_equal "developed by <span class=\"small-caps\">NCSA</span>.", Typeset.typeset("developed by NCSA.", OptNoHyphenate)
36
+ assert_equal "mCAT", Typeset.typeset("mCAT", OptNoHyphenate)
37
+ assert_equal "<span class=\"small-caps\">CAT</span>-5", Typeset.typeset("CAT-5", {:disable => [:hanging_punctuation, :hyphenate]})
38
+ end
39
+
40
+ def test_ligatures
41
+ assert_equal "ffoo", Typeset.typeset("ffoo")
42
+ assert_equal "fioo", Typeset.typeset("fioo")
43
+ assert_equal "floo", Typeset.typeset("floo")
44
+ assert_equal "ffioo", Typeset.typeset("ffioo")
45
+ assert_equal "ffloo", Typeset.typeset("ffloo")
46
+
47
+ # Ensure extended ligatures don't work by default...
48
+ assert_equal "flue", Typeset.typeset("flue")
49
+ assert_equal "due", Typeset.typeset("due")
50
+
51
+ # ...but do work on request
52
+ assert_equal "stoo", Typeset.typeset("stoo", {:ligatures => [Typeset::DefaultLigatures, 'st'].flatten})
53
+ assert_equal "ᵫoo", Typeset.typeset("ueoo", {:ligatures => [Typeset::DefaultLigatures, 'ue'].flatten})
54
+ end
55
+
56
+ def test_hyphenate
57
+ assert_equal "do hy\u00ADphen\u00ADation", Typeset.typeset("do hyphenation")
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtypeset
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Trevor Fountain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: text-hyphen
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 5.8.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 5.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jeweler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.0.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A pure Ruby implementation of Typeset.js, an HTML pre-processor for web
126
+ typography
127
+ email: trevor@texasexpat.net
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files:
131
+ - LICENSE.txt
132
+ - README.md
133
+ files:
134
+ - ".document"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - VERSION
141
+ - demo/demo.rb
142
+ - demo/raw.html
143
+ - demo/style.css
144
+ - demo/typeset.css
145
+ - index.html
146
+ - lib/typeset.rb
147
+ - lib/typeset/hanging_punctuation.rb
148
+ - lib/typeset/hyphenate.rb
149
+ - lib/typeset/ligatures.rb
150
+ - lib/typeset/punctuation.rb
151
+ - lib/typeset/quotes.rb
152
+ - lib/typeset/small_caps.rb
153
+ - lib/typeset/spaces.rb
154
+ - test/helper.rb
155
+ - test/test_typeset.rb
156
+ homepage: http://github.com/doches/rtypeset
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.4.7
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: An HTML pre-processor for web typography
180
+ test_files: []