aspec_rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ Encoding.default_external = Encoding::UTF_8
5
+ Encoding.default_internal = Encoding::UTF_8
6
+
7
+ # This marker should be in the target document where the TOC will be placed.
8
+ marker = '---TOC---'
9
+ gendir = 'generated-docs' # TODO: - do not hardcode
10
+
11
+ # Open a <ul> element
12
+ toc = %{<input type="text" id="pagesearch" onkeyup="search()" placeholder="Filter chapters"
13
+ style="margin-left: 18px;">
14
+ <i class="fa fa-times" aria-hidden="true" style="display:none;" id="clear"></i>
15
+ <ul class="nav" id="treeview">
16
+
17
+ }
18
+
19
+ html_files = Dir.glob("#{gendir}/**/*.html")
20
+ anchors, sections, appendices = [], [], []
21
+
22
+ html_files.each do |file|
23
+ next if file == "#{gendir}/search.html" || file[%r{^#{gendir}\/index}]
24
+ page = Nokogiri::HTML(open(file))
25
+ filename = file.sub(%r{^#{gendir}\/}, '')
26
+
27
+ # All Asciidoc files will have a <h2> element as the document name
28
+ # when sectnums attribute is enabled, this can be used for sorting
29
+ pagetitle = page.xpath('//h2').text
30
+
31
+ # Collect all heading elements, exclude 'panel-title' class used in Requirements
32
+ # TODO - check for other heading elements that are not section titles.
33
+ # It may be wise to search by xpath for heading elements directly after 'sect1-6' class divs
34
+ hs = page.xpath("//h2 | //h3[not(@class='panel-title')] | //h4 | //h5").collect
35
+
36
+ if pagetitle[/^Appendix/]
37
+ # Create an array of appendices
38
+ appendices.push([hs, pagetitle, filename])
39
+ elsif pagetitle[/^\d+/]
40
+ # Create an array of section titles by chapter number
41
+ sections.push([hs, pagetitle, filename])
42
+ end
43
+ end
44
+
45
+ # Sort Sections by number
46
+ sections.sort_by! { |_content, title| title.scan(/^\d+/).first.to_i }
47
+
48
+ # Sort Appendices Array
49
+ appendices.sort_by! { |_content, title| title }
50
+
51
+ # Push Appendices to end of Sections array
52
+ appendices.each do |content, title, filename|
53
+ sections.push([content, title, filename])
54
+ end
55
+
56
+ # For each Section (HTML page), create an array of anchors
57
+ sections.each do |sect, _title, filename|
58
+ sect.each do |content|
59
+ anchors.push([filename, content.attributes['id'].to_s, content.content.to_s, content.name.delete('h').to_i])
60
+ end
61
+ end
62
+
63
+ i = 0
64
+ prev_level = 0
65
+
66
+ # For each anchor, create a html list element
67
+ anchors.each do |file, id, text, level|
68
+ li = "<li><a href=\"#{file}##{id}\">#{text}</a></li>\n"
69
+
70
+ # If there are subsections, add a nested <ul> element
71
+ if level > prev_level
72
+ if i != 0
73
+ toc = toc.chomp("</li>\n")
74
+ toc += " <a href=\"#\" data-toggle=\"collapse\" data-target=\"#tocnav_#{id}\"><i class=\"fa fa-plus-square\"
75
+ aria-hidden=\"true\"></i></a>
76
+ <ul>
77
+ <div id=\"tocnav_#{id}\" class=\"collapse\">
78
+ <li><a href=\"#{file}##{id}\">#{text}</a></li>\n"
79
+ li = ''
80
+ end
81
+
82
+ # Close nested <ul> elements
83
+ elsif level < prev_level
84
+ diff = prev_level - level
85
+ diff.times { toc += "</div></ul>\n" }
86
+ end
87
+ i += 1
88
+ toc += li
89
+ # assign a variable with current level to compare in next iteration
90
+ prev_level = level
91
+ end
92
+
93
+ # Close the toc
94
+ toc += '</ul>'
95
+
96
+ html_files.each do |fi|
97
+ file = fi.sub(%r{#{gendir}\/}, '')
98
+ thistoc = toc.gsub(/#{file}/, '')
99
+ data = File.read(fi)
100
+ File.open(fi, 'w') do |f|
101
+ modified = data.sub(marker, thistoc)
102
+ f.write(modified)
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aspec_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - bsmith-n4
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.15.4
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.15.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 12.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 12.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: asciidoctor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.0
69
+ description: |-
70
+ This plugin is a group of Asciidoctor extensions that perform directory walking,
71
+ resolving the location of titles and anchors in all adoc files so that inter-document
72
+ cross-references in a Jekyll project are resolved automatically. Also included are some
73
+ custom macros and blocks that are useful for techinical writing.
74
+ email:
75
+ - brian.smith@numberfour.eu
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - ".rubocop.yml"
82
+ - ".travis.yml"
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - README.md
86
+ - Rakefile
87
+ - aspec_rb.gemspec
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/aspec_rb.rb
91
+ - lib/aspec_rb/version.rb
92
+ - lib/extensions/autoxrefs.rb
93
+ - lib/extensions/definition_block.rb
94
+ - lib/extensions/inline_callout_macro.rb
95
+ - lib/extensions/inline_cwiki_macro.rb
96
+ - lib/extensions/inline_repo_macro.rb
97
+ - lib/extensions/inline_task_macro.rb
98
+ - lib/extensions/postprocessor.rb
99
+ - lib/extensions/req_refs.rb
100
+ - lib/extensions/requirement_appendix.rb
101
+ - lib/extensions/requirement_block.rb
102
+ - lib/extensions/todo_block.rb
103
+ - lib/extensions/utils/block.rb
104
+ - lib/extensions/utils/labels.rb
105
+ - lib/extensions/utils/scanner.rb
106
+ - lib/html_chunker.rb
107
+ - lib/postprocessors/fulltext_search.rb
108
+ - lib/postprocessors/generate_toc.rb
109
+ homepage: https://github.com/bsmith-n4/aspec_rb
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '2.0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.12
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Asciidoctor extensions for use as a Jekyll plugin
133
+ test_files: []