Class: RubyPants
- Inherits:
-
String
- Object
- String
- RubyPants
- Defined in:
- lib/rubypants-unicode/version.rb,
lib/rubypants-unicode/rubypants-unicode.rb
Constant Summary collapse
- VERSION =
"0.2.5"
Instance Method Summary collapse
-
#initialize(string, options = [2]) ⇒ RubyPants
constructor
Create a new RubyPants instance with the text in
string
. -
#to_html ⇒ Object
Apply SmartyPants transformations.
Constructor Details
#initialize(string, options = [2]) ⇒ RubyPants
Create a new RubyPants instance with the text in string
.
Allowed elements in the options array:
- 0
-
do nothing
- 1
-
enable all, using only em-dash shortcuts
- 2
-
enable all, using old school en- and em-dash shortcuts (default)
- 3
-
enable all, using inverted old school en and em-dash shortcuts
- -1
-
stupefy (translate HTML entities to their ASCII-counterparts)
If you don’t like any of these defaults, you can pass symbols to change RubyPants’ behavior:
:quotes
-
quotes
:backticks
-
backtick quotes (“double” only)
:allbackticks
-
backtick quotes (“double” and ‘single’)
:dashes
-
dashes
:oldschool
-
old school dashes
:inverted
-
inverted old school dashes
:ellipses
-
ellipses
:convertquotes
-
convert
"
entities to"
for Dreamweaver users :stupefy
-
translate RubyPants HTML entities to their ASCII counterparts.
207 208 209 210 |
# File 'lib/rubypants-unicode/rubypants-unicode.rb', line 207 def initialize(string, =[2]) super string @options = [*] end |
Instance Method Details
#to_html ⇒ Object
Apply SmartyPants transformations.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/rubypants-unicode/rubypants-unicode.rb', line 213 def to_html do_quotes = do_backticks = do_dashes = do_ellipses = do_stupify = nil convert_quotes = false if @options.include? 0 # Do nothing. return self elsif @options.include? 1 # Do everything, turn all options on. do_quotes = do_backticks = do_ellipses = true do_dashes = :normal elsif @options.include? 2 # Do everything, turn all options on, use old school dash shorthand. do_quotes = do_backticks = do_ellipses = true do_dashes = :oldschool elsif @options.include? 3 # Do everything, turn all options on, use inverted old school # dash shorthand. do_quotes = do_backticks = do_ellipses = true do_dashes = :inverted elsif @options.include?(-1) do_stupefy = true else do_quotes = @options.include? :quotes do_backticks = @options.include? :backticks do_backticks = :both if @options.include? :allbackticks do_dashes = :normal if @options.include? :dashes do_dashes = :oldschool if @options.include? :oldschool do_dashes = :inverted if @options.include? :inverted do_ellipses = @options.include? :ellipses convert_quotes = @options.include? :convertquotes do_stupefy = @options.include? :stupefy end # 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|style|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!(/"/, '"') if convert_quotes if do_dashes t = educate_dashes t if do_dashes == :normal t = educate_dashes_oldschool t if do_dashes == :oldschool t = educate_dashes_inverted t if do_dashes == :inverted end t = educate_ellipses t if do_ellipses # Note: backticks need to be processed before quotes. if do_backticks t = educate_backticks t t = educate_single_backticks t if do_backticks == :both end if do_quotes if t == "'" # Special case: single-character ' token if prev_token_last_char =~ /\S/ t = "’" else t = "‘" end elsif t == '"' # Special case: single-character " token if prev_token_last_char =~ /\S/ t = "”" else t = "“" end else # Normal case: t = educate_quotes t end end t = stupefy_entities t if do_stupefy end prev_token_last_char = last_char result << t end } # Done result end |