Improve blank element detection and fix treatment of tables

This commit is contained in:
RunasSudo 2016-10-12 20:45:18 +10:30
parent 2c7407ebfd
commit f222ad1fab
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
1 changed files with 36 additions and 10 deletions

View File

@ -36,6 +36,25 @@ module Jekyll
return body.inner_html return body.inner_html
end end
def is_blank(doc)
if doc.is_a?(Nokogiri::XML::Text)
if doc.content.strip().length == 0
return true
end
end
if doc.children.length == 0
return true
end
doc.children.each do |child|
if !is_blank(child)
return false
end
end
return true
end
def smart_truncate_doc(doc, num_words) def smart_truncate_doc(doc, num_words)
if doc.is_a?(Nokogiri::XML::Text) if doc.is_a?(Nokogiri::XML::Text)
if num_words > 0 if num_words > 0
@ -61,14 +80,14 @@ module Jekyll
return smart_truncate_table(doc, num_words) return smart_truncate_table(doc, num_words)
else else
if num_words > 0 if num_words > 0
children_orig = doc.children.length was_blank = is_blank(doc)
count = 0 count = 0
doc.children.each do |child| doc.children.each do |child|
count += smart_truncate_doc(child, num_words - count) count += smart_truncate_doc(child, num_words - count)
end end
if doc.children.length == 0 && children_orig != 0 if is_blank(doc) && !was_blank
doc.remove() doc.remove()
end end
@ -95,16 +114,23 @@ module Jekyll
return count return count
end end
else else
count = 0 if num_words > 0
doc.children.each do |child| was_blank = is_blank(doc)
count += smart_truncate_table(child, num_words - count)
end count = 0
doc.children.each do |child|
if doc.children.length == 0 count += smart_truncate_table(child, num_words - count)
end
if is_blank(doc) && !was_blank
doc.remove()
end
return count
else
doc.remove() doc.remove()
return 0
end end
return count
end end
end end
end end