[gnome-devel-docs/gnome3-hig] Add some typographical love



commit a55a9e390d84ca2b517bb2c66f96798edcf641ce
Author: Patryk Zawadzki <patrys room-303 com>
Date:   Fri Jan 20 00:35:41 2012 +0100

    Add some typographical love

 hig3/config/site.rb                 |    2 +
 hig3/lib/haml/helpers/typography.rb |   97 +++++++++++++++++++++++++++++++++++
 hig3/src/layouts/default.haml       |    3 +-
 hig3/src/stylesheets/hig.scss       |    4 ++
 4 files changed, 105 insertions(+), 1 deletions(-)
---
diff --git a/hig3/config/site.rb b/hig3/config/site.rb
index 19adbda..5d351d5 100644
--- a/hig3/config/site.rb
+++ b/hig3/config/site.rb
@@ -1,5 +1,7 @@
 # Default is 3000
 # configuration.preview_server_port = 3000
+
+require 'haml/helpers/typography'
  
 # Default is localhost
 #configuration.preview_server_host = "kratos"
diff --git a/hig3/lib/haml/helpers/typography.rb b/hig3/lib/haml/helpers/typography.rb
new file mode 100644
index 0000000..d685135
--- /dev/null
+++ b/hig3/lib/haml/helpers/typography.rb
@@ -0,0 +1,97 @@
+require 'rubypants'
+
+class Typography < RubyPants
+
+    def to_html
+        # Parse the HTML
+        tokens = tokenize
+
+        # Keep track of when we're inside <pre> or <code> tags.
+        in_pre = false
+
+        # Here is the result stored in.
+        result = ""
+
+        # This is a cheat, used to get some context for one-character
+        # tokens that consist of just a quote char. What we do is remember
+        # the last character of the previous text token, to use as context
+        # to curl single- character quote tokens correctly.
+        prev_token_last_char = nil
+
+        tokens.each { |token|
+            if token.first == :tag
+                result << token[1]
+                if token[1] =~ %r!<(/?)(?:pre|code|kbd|script|math)[\s>]!
+                    in_pre = ($1 != "/")  # Opening or closing tag?
+                end
+            else
+                t = token[1]
+
+                # Remember last char of this token before processing.
+                last_char = t[-1].chr
+
+                unless in_pre
+                    t = process_escapes t
+
+                    t.gsub!(/&quot;/, '"')
+
+                    t = educate_dashes t
+                    t = educate_dashes_oldschool t
+                    t = educate_dashes_inverted t
+
+                    t = educate_ellipses t
+
+                    # Note: backticks need to be processed before quotes.
+                    t = educate_backticks t
+                    t = educate_single_backticks t
+
+                    if t == "'"
+                        # Special case: single-character ' token
+                        if prev_token_last_char =~ /\S/
+                            t = "&#8217;"
+                        else
+                            t = "&#8216;"
+                        end
+                    elsif t == '"'
+                        # Special case: single-character " token
+                        if prev_token_last_char =~ /\S/
+                            t = "&#8221;"
+                        else
+                            t = "&#8220;"
+                        end
+                    else
+                        # Normal case:
+                        t = educate_quotes t
+                    end
+
+                    t = stupefy_entities t
+                end
+
+                prev_token_last_char = last_char
+                result << t
+            end
+        }
+
+        # Done
+        gnome widows(result)
+    end
+
+    protected
+
+    def gnome(text)
+        text.gsub(/(GNOME(\s+3)?)/,'<span class="gnome">\1</span>')
+    end
+
+    def widows(text)
+        text.gsub(/(\s+)((<[^>]+>\s*)?[^<>\s]+(<\/(a|em|span|strong)[^>]*>\s*)*(<\/(p|h[1-6]|li|dt|dd)|$))/, '&nbsp;\2')
+    end
+end
+
+module Haml::Helpers
+#    include Haml::Filters::Base
+
+    def typography(&block)
+        text = capture_haml(&block)
+        Typography.new(text).to_html
+    end
+end
diff --git a/hig3/src/layouts/default.haml b/hig3/src/layouts/default.haml
index c228247..cc64f83 100644
--- a/hig3/src/layouts/default.haml
+++ b/hig3/src/layouts/default.haml
@@ -18,7 +18,8 @@
         .menu
           = partial('menu')
       %article.main
-        = yield
+        =typography do
+          = yield
     #footerpositioner
       %hr.footerart
       #footer
diff --git a/hig3/src/stylesheets/hig.scss b/hig3/src/stylesheets/hig.scss
index 2771454..473ba8d 100644
--- a/hig3/src/stylesheets/hig.scss
+++ b/hig3/src/stylesheets/hig.scss
@@ -55,6 +55,10 @@ h1 { font-size: 200%;  }
   @include text-shadow(0,1px,0,#fff);
 }
 
+.gnome {
+  white-space: nowrap;
+}
+
 .gnomelink {
   width: 960px;
   margin: 0 auto;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]