# File lib/rubypants.rb, line 80
  def to_html
    do_quotes = do_backticks = do_dashes = do_ellipses = do_stupify = nil
    convert_quotes = prevent_breaks = nil

    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
    end

    # Explicit flags override numeric flag groups.
    do_quotes      = true if @options.include?(:quotes)
    do_backticks   = true if @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)
    prevent_breaks = true if @options.include?(:prevent_breaks)
    do_ellipses    = true if @options.include?(:ellipses)
    convert_quotes = true if @options.include?(:convertquotes)
    do_stupefy     = true if @options.include?(:stupefy)

    # Parse the HTML
    tokens = tokenize

    # Keep track of when we're inside <pre> or <code> tags.
    in_pre = nil

    # 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 do |token|
      if token.first == :tag
        result << token[1]
        if token[1].end_with? '/>'
          # ignore self-closing tags
        elsif token[1] =~ %r!\A<(/?)(pre|code|kbd|script|style|math)[\s>]!
          if $1 == '' && ! in_pre
            in_pre = $2
          elsif $1 == '/' && $2 == in_pre
            in_pre = nil
          end
        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;/, '"')  if convert_quotes

          if do_dashes
            t = educate_dashes t, prevent_breaks            if do_dashes == :normal
            t = educate_dashes_oldschool t, prevent_breaks  if do_dashes == :oldschool
            t = educate_dashes_inverted t, prevent_breaks   if do_dashes == :inverted
          end

          t = educate_ellipses t, prevent_breaks  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 = entity(:single_right_quote)
              else
                t = entity(:single_left_quote)
              end
            elsif t == '"'
              # Special case: single-character " token
              if prev_token_last_char =~ /\S/
                t = entity(:double_right_quote)
              else
                t = entity(:double_left_quote)
              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
    end

    # Done
    result
  end