From 53b2b288ac996f1f645b4eb3e2f13a6af91ace7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 18 Aug 2018 16:44:45 +0200 Subject: [PATCH] Generate the release notes' toc --- .travis/render_release_notes.rb | 1 + docs/_plugins/tocmaker_block.rb | 51 +++++++++++++++++++++++++++++++++ docs/pages/release_notes.md | 10 ++----- 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 docs/_plugins/tocmaker_block.rb diff --git a/.travis/render_release_notes.rb b/.travis/render_release_notes.rb index d8acab0e74..dc0eb0877d 100755 --- a/.travis/render_release_notes.rb +++ b/.travis/render_release_notes.rb @@ -13,6 +13,7 @@ require "safe_yaml" # include some custom liquid extensions require_relative "../docs/_plugins/rule_tag" +require_relative "../docs/_plugins/tocmaker_block" require_relative "../docs/_plugins/custom_filters" # explicitly setting safe mode to get rid of the warning diff --git a/docs/_plugins/tocmaker_block.rb b/docs/_plugins/tocmaker_block.rb new file mode 100644 index 0000000000..e62ee49374 --- /dev/null +++ b/docs/_plugins/tocmaker_block.rb @@ -0,0 +1,51 @@ +# Generates a table of contents based on markdown headers in the body +# +# The block's arg may be an int describing the maximum depth at which +# headers are added to the toc + +class TocMakerBlock < Liquid::Block + # include Enumerable + + def initialize(tag_name, arg, tokens) + super + @max_depth = arg.to_s.empty? ? 100 : arg.to_i + @body = tokens + end + + def to_internal_link(header) + url = header.downcase.gsub(/\s+/, "-") + + "[#{header}](##{url})" + end + + def render(context) + + contents = @body.render(context) + + + headers = contents.lines.map {|l| + if /^(#+)\s+(\S.*)$/ =~ l + [$1.length, $2] + end + }.compact + + min_indent = headers.min_by {|t| t[0]}[0] + + headers = headers.map {|t| + actual_depth = t[0] - min_indent + if actual_depth < @max_depth then + + indent = " " * actual_depth + + "#{indent}* #{to_internal_link(t[1])}" + end + }.compact + + headers.unshift("### Table Of Contents\n") + + headers.join("\n") + contents + end +end + + +Liquid::Template.register_tag('tocmaker', TocMakerBlock) \ No newline at end of file diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index a324c2ee28..efa8e49230 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -10,13 +10,7 @@ The PMD team is pleased to announce PMD {{ site.pmd.version }}. This is a {{ site.pmd.release_type }} release. -### Table Of Contents - -* [New and noteworthy](#new-and-noteworthy) - * [New Rules](#new-rules) -* [Fixed Issues](#fixed-issues) -* [API Changes](#api-changes) -* [External Contributions](#external-contributions) +{% tocmaker %} ### New and noteworthy @@ -77,3 +71,5 @@ This is a {{ site.pmd.release_type }} release. * [#1289](https://github.com/pmd/pmd/pull/1289): \[java] UselessParentheses: Fix false positive with assignments - [cobratbq](https://github.com/cobratbq) * [#1290](https://github.com/pmd/pmd/pull/1290): \[docs] \[GSoC] Create the documentation about pmdtester - [BBG](https://github.com/djydewang) * [#1256](https://github.com/pmd/pmd/pull/1256): \[java] #940 Avoid JUnit 4 false positives for JUnit 5 tests - [Alex Shesterov](https://github.com/vovkss) + +{% endtocmaker %}