Skip to content

Reduce the number of useless new lines in html doc #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
benoittgt opened this issue Mar 31, 2020 · 3 comments · Fixed by #775
Closed

Reduce the number of useless new lines in html doc #759

benoittgt opened this issue Mar 31, 2020 · 3 comments · Fixed by #775

Comments

@benoittgt
Copy link
Contributor

Hello

With rdoc 6.2.1 and the ruby code:

class Shape
  def initialize(polyline)
  end
end

I get an html with a lot of extra lines and trailing whitespace.

Capture d’écran 2020-03-31 à 10 26 05

Is it something that can be fixed?

I can look at it

@benoittgt
Copy link
Contributor Author

I looked a it a little bit. It's probably related to ERB because the template have empty lines but is not responsible of all those extra lines.

I am on Ruby 2.7 and the trim mode seems ok.

I will keep digging.

@benoittgt
Copy link
Contributor Author

It is more related to an ERB behavior. I don't see an easy way at the moment to do not create empty lines and don't break indentation.

require 'erb'

expected = %{
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>
}

template_unindented = %{
<ul>
<% list = [1,2,3] %>
<% for item in list %>
<% if item %>
<li><%= item %></li>
<% end %>
<% end %>
</ul>
 } # space is needed for correct formating

template_indented = %{
<ul>
  <% list = [1,2,3] %>
  <% for item in list %>
  <% if item %>
  <li><%= item %></li>
  <% end %>
  <% end %>
</ul>
}

templates = [
  {
    name: 'template_unindented',
    trim_mode: '<>',
    content: template_unindented
  },
  {
    name: 'template_indented',
    trim_mode: '<>',
    content: template_indented
  },
  {
    name: 'template_indented',
    trim_mode: '%>',
    content: template_indented
  },
  {
    name: 'template_indented',
    trim_mode: '-',
    content: template_indented
  }
]

templates.each do |template|
  puts "🎲  With trim_mode: #{template[:trim_mode]}, for #{template[:name]}\n\n"
  pp ERB.new(template[:content], trim_mode: template[:trim_mode]).result
  puts
end

puts "🎲  Expected\n\n"

pp expected
🎲  With trim_mode: <>, for template_unindented

"\n" +
"<ul>\n" +
"<li>1</li>\n" +
"<li>2</li>\n" +
"<li>3</li>\n" +
"</ul>\n" +
" "

🎲  With trim_mode: <>, for template_indented

"\n" +
"<ul>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>1</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>2</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>3</li>\n" +
"  \n" +
"  \n" +
"</ul>\n"

🎲  With trim_mode: %>, for template_indented

"\n" +
"<ul>\n" +
"        <li>1</li>\n" +
"        <li>2</li>\n" +
"        <li>3</li>\n" +
"    </ul>\n"

🎲  With trim_mode: -, for template_indented

"\n" +
"<ul>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>1</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>2</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>3</li>\n" +
"  \n" +
"  \n" +
"</ul>\n"

🎲  Expected

"\n" +
"<ul>\n" +
"  <li>1</li>\n" +
"  <li>2</li>\n" +
"  <li>3</li>\n" +
"</ul>\n"

@benoittgt
Copy link
Contributor Author

Ok so with the last version and the trim_mode to '-' for "- omit blank lines ending in -%>" if I change the template, I am able to remove some the empty lines.

One example in class template:

--- a/lib/rdoc/generator/template/darkfish/class.rhtml
+++ b/lib/rdoc/generator/template/darkfish/class.rhtml
@@ -122,28 +122,26 @@
         <% end %>

         <div class="method-description">
-          <% if method.comment then %>
+          <%- if method.comment then -%>
           <%= method.description.strip %>
-          <% else %>
+          <%- else -%>
           <p class="missing-docs">(Not documented)
-          <% end %>
-          <% if method.calls_super then %>
+          <%- end -%>
+          <%- if method.calls_super then -%>
             <div class="method-calls-super">
               Calls superclass method
               <%=
                   method.superclass_method ?
                   method.formatter.link(method.superclass_method.full_name, method.superclass_method.full_name) : nil
-              %>
+              -%>
             </div>
-          <% end %>
-
-          <% if method.token_stream then %>
-          <div class="method-source-code" id="<%= method.html_name %>-source">
-            <pre><%= method.markup_code %></pre>
+          <%- end -%>
+          <%- if method.token_stream then -%>
+          <div class="method-source-code" id="<%= method.html_name -%>-source">
+            <pre><%= method.markup_code -%></pre>
           </div>
-          <% end %>
+          <%- end -%>
         </div>
-

Produce this diff in the html:

--- a/Shape-master.html
+++ b/Shape-new.html
@@ -129,19 +129,12 @@

         <div class="method-description">

-
-
-
-
-
           <div class="method-source-code" id="new-source">
             <pre><span class="ruby-comment"># File shape.rb, line 2</span>
 <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">polyline</span>)
 <span class="ruby-keyword">end</span></pre>
           </div>
-
         </div>
-

I still have a blank line, but if I remove it I will have an indentation issue:
For the line, if change it like :

-          <%= method.description.strip%>
+          <%= method.description.strip -%> 

I will jump from :

"        <div class=\"method-description\">\n" +
"          \n" +
"          <div class=\"method-source-code\" id=\"new-source\">\n" +
"            <pre><span class=\"ruby-comment\">\n" +

To :

"        <div class=\"method-description\">\n" +
"                    <div class=\"method-source-code\" id=\"new-source\">\n" +
"            <pre><span class=\"ruby-comment\">\n" +

So it is possible to fix some of the extra lines, but it requires to pass on all templates.

Do you want this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant