]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Don't show `.bd-*` classes to the user (#21443)
authorStarsam80 <samraskauskas@gmail.com>
Tue, 27 Dec 2016 22:15:22 +0000 (15:15 -0700)
committerMark Otto <markd.otto@gmail.com>
Tue, 27 Dec 2016 22:15:22 +0000 (14:15 -0800)
* Don't show `bd-*` classes to the user

* Add comments and another regex

docs/_plugins/highlight_alt.rb

index bdcada251791f3a4cbe07c99cd6985b7f37f7ad1..8a86a2202825e4b9f4fc39a6cff95063804c6a0d 100644 (file)
@@ -57,16 +57,26 @@ eos
       def example(output)
         "<div class=\"bd-example\" data-example-id=\"#{@options[:id]}\">\n#{output}\n</div>"
       end
-    
+
       def remove_holderjs(code)
         code = code.gsub(/data-src="holder.js.+?"/, 'src="..."')
       end
 
+      def remove_example_classes(code)
+        # Find `bd-` classes and remove them from the highlighted code. Because of how this regex works, it will also
+        # remove classes that are after the `bd-` class. While this is a bug, I left it because it can be helpful too.
+        # To fix the bug, replace `(?=")` with `(?=("|\ ))`.
+        code = code.gsub(/(?!class=".)\ *?bd-.+?(?=")/, "")
+        # Find empty class attributes after the previous regex and remove those too.
+        code = code.gsub(/\ class=""/, "")
+      end
+
       def render_rouge(code)
         require 'rouge'
         formatter = Rouge::Formatters::HTML.new(line_numbers: @options[:linenos], wrap: false)
         lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
         code = remove_holderjs(code)
+        code = remove_example_classes(code)
         code = formatter.format(lexer.lex(code))
         "<div class=\"highlight\"><pre>#{code}</pre></div>"
       end