Ver Fonte

[gnuplot] Dedent code blocks

Jed Fox há 5 anos atrás
pai
commit
5adb7a7b32
1 ficheiros alterados com 23 adições e 0 exclusões
  1. 23 0
      lib/docs/filters/gnuplot/clean_html.rb

+ 23 - 0
lib/docs/filters/gnuplot/clean_html.rb

@@ -36,6 +36,10 @@ module Docs
           node.remove if node.content.strip.empty?
         end
 
+        css('pre').each do |node|
+          node.content = dedent(node.content)
+        end
+
         # links generated are of the form (NB: some might have been removed):
         # <B>{text} (p.&nbsp;<A HREF="{target}"><IMG  ALT="[*]" SRC="crossref.png"></A>)<A NAME="{anchor}"></A></B>
         # transform to <b><a href="{target}>{text}</a></b>
@@ -56,6 +60,25 @@ module Docs
 
         doc
       end
+
+      private
+      def dedent string
+        lines = string.split "\n"
+        indent = lines.reduce Float::INFINITY do |least, line|
+          if line == ''
+            least
+          else
+            [least, line.index(line.lstrip)].min
+          end
+        end
+        if indent == Float::INFINITY
+          string
+        else
+          lines
+            .map { |line| line[indent..] || '' }
+            .join("\n")
+        end
+      end
     end
   end
 end