Skip to content

Commit 889bbf9

Browse files
author
synapticloop
committed
added in toggle table of contents links
1 parent d503254 commit 889bbf9

File tree

9 files changed

+234
-96
lines changed

9 files changed

+234
-96
lines changed

README.md

Lines changed: 114 additions & 75 deletions
Large diffs are not rendered by default.

build-test.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ buildscript {
1313
}
1414

1515
dependencies {
16-
classpath 'synapticloop:documentr:2.2.0'
16+
classpath 'synapticloop:documentr:2.3.0'
1717
}
1818
}
1919

@@ -36,7 +36,7 @@ group = 'synapticloop'
3636
archivesBaseName = 'documentr'
3737
description = """documentation (README) generator for projects in both markdown and asciidoc - this utilises the templar templating language - including generation of table of contents"""
3838

39-
version = '2.2.0'
39+
version = '2.3.0'
4040

4141
sourceCompatibility = 1.7
4242
targetCompatibility = 1.7

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ buildscript {
1313
}
1414

1515
dependencies {
16-
classpath 'synapticloop:documentr:2.2.0'
16+
classpath 'synapticloop:documentr:2.3.0'
1717
}
1818
}
1919

@@ -36,7 +36,7 @@ group = 'synapticloop'
3636
archivesBaseName = 'documentr'
3737
description = """documentation (README) generator for projects in both markdown and asciidoc - this utilises the templar templating language - including generation of table of contents"""
3838

39-
version = '2.2.0'
39+
version = '2.3.0'
4040

4141
sourceCompatibility = 1.7
4242
targetCompatibility = 1.7

documentr.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
{ "type": "markup", "value": "\n\n# Table of Contents\n\n" },
1414

1515
{ "type": "toc", "value": "2" },
16+
{ "type": "toclinks", "value": "true" },
17+
18+
{ "type": "file", "value":"src/docs/table-of-contents.md" },
1619

1720
{ "type":"inbuilt", "value":"project-name" },
1821
{ "type":"inbuilt", "value":"project-description" },

src/docs/table-of-contents.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Generating the table of contents
3+
4+
`documentr` can automatically generate the table fo contents for your documentation,
5+
simply by entering the following line into you `documentr.json` file:
6+
7+
```
8+
{ "type": "markup", "value": "\n\n# Table of Contents\n\n" },
9+
10+
{ "type": "toc", "value": "2" },
11+
{ "type": "toclinks", "value": "true" },
12+
```
13+
By default, no title is generated - you will need to include one as simple markup.
14+
15+
This will generate both the table of contents and the links to all of the headers.
16+
17+
The `{ "type": "toc", "value": "2" }` line will generate links up to `h2` elements, by
18+
default this will generate up to header level 6.
19+
20+
The `{ "type": "toclinks", "value": "true" },` line will generate links to he headers,
21+
and will only work if `"type": "toc"` is also included.
22+
23+
> By default links are not generated. Unfortunately the developer of the markdown processor that is in use, does not distinguish block quote level elements correctly such that anything that looks like a header included in a blockquote will also be incorrectly identified as a header. This is why the links are not automatically generated by default.
24+
25+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package synapticloop.documentr.bean;
2+
3+
/*
4+
* Copyright (c) 2016 Synapticloop.
5+
*
6+
* All rights reserved.
7+
*
8+
* This code may contain contributions from other parties which, where
9+
* applicable, will be listed in the default build file for the project
10+
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
11+
*
12+
* This source code and any derived binaries are covered by the terms and
13+
* conditions of the Licence agreement ("the Licence"). You may not use this
14+
* source code or any derived binaries except in compliance with the Licence.
15+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
16+
* this source code or binaries.
17+
*/
18+
19+
public class StartEndBean {
20+
21+
private int start;
22+
private int end;
23+
24+
public StartEndBean(int start, int end) {
25+
this.start = start;
26+
this.end = end;
27+
}
28+
29+
public int getStart() { return this.start; }
30+
31+
public int getEnd() { return this.end; }
32+
33+
}

src/main/java/synapticloop/documentr/generator/Generator.java

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public class Generator {
6464
private static final int TYPE_TEMPLAR = 2;
6565
private static final int TYPE_TEMPLATE = 3;
6666
private static final int TYPE_MARKUP = 4;
67-
private static final int TYPE_TOC = 5;
67+
private static final int TYPE_MARKDOWN = 5;
68+
private static final int TYPE_TOC = 6;
69+
private static final int TYPE_TOCBACKTOTOP = 7;
70+
private static final int TYPE_TOCLINKS= 8;
6871

6972
private static final String CONTEXT_CONFIGURATION_BEANS = "configurationBeans";
7073
private static final String CONTEXT_CONFIGURATIONS = "configurations";
@@ -78,7 +81,10 @@ public class Generator {
7881
TYPE_LOOKUP.put("templar", TYPE_TEMPLAR);
7982
TYPE_LOOKUP.put("template", TYPE_TEMPLATE);
8083
TYPE_LOOKUP.put("markup", TYPE_MARKUP);
84+
TYPE_LOOKUP.put("markdown", TYPE_MARKDOWN);
8185
TYPE_LOOKUP.put("toc", TYPE_TOC);
86+
TYPE_LOOKUP.put("toclinks", TYPE_TOCLINKS);
87+
TYPE_LOOKUP.put("tocbacktop", TYPE_TOCBACKTOTOP);
8288

8389
SPACING_LOOKUP.put(1, " - ");
8490
SPACING_LOOKUP.put(2, " - ");
@@ -94,8 +100,10 @@ public class Generator {
94100
private boolean verbose = false;
95101

96102
// table of content variables
97-
private boolean hasToc = false;
98103
private int tocLevel = 6;
104+
private boolean hasToc = false;
105+
private boolean hasTocBackToTop = false;
106+
private boolean hasTocLinks = false;
99107

100108
private final TemplarContext templarContext = new TemplarContext();
101109
private List<ConfigurationBean> configurationBeans = new ArrayList<ConfigurationBean>();
@@ -177,7 +185,7 @@ public void generate() throws DocumentrException {
177185
throw new DocumentrException(String.format("Unknown type of '%s'", type));
178186
}
179187

180-
String value = templateObject.getString(KEY_VALUE);
188+
String value = templateObject.optString(KEY_VALUE, "");
181189
String pathname = documentrJsonFile.getParent() + "/" + value;
182190
switch(TYPE_LOOKUP.get(type)) {
183191
case TYPE_FILE:
@@ -186,6 +194,7 @@ public void generate() throws DocumentrException {
186194
stringBuilder.append("\npre}\n");
187195
break;
188196
case TYPE_MARKUP:
197+
case TYPE_MARKDOWN:
189198
stringBuilder.append("\n");
190199
stringBuilder.append(value.replaceAll("\\{", "\\{\\{").replaceAll("\\n", "\\{\\\\n\\}").replaceAll("\\t", "\\{\\\\t\\}"));
191200
stringBuilder.append("\n");
@@ -221,6 +230,18 @@ public void generate() throws DocumentrException {
221230
// ignore - will stay at 6
222231
}
223232
break;
233+
case TYPE_TOCBACKTOTOP:
234+
if(value.equalsIgnoreCase("true")) {
235+
hasTocBackToTop = true;
236+
stringBuilder.insert(0, "<a name=\"top\"></a>");
237+
}
238+
break;
239+
case TYPE_TOCLINKS:
240+
if(value.equalsIgnoreCase("true")) {
241+
hasTocLinks = true;
242+
}
243+
break;
244+
224245
default:
225246
throw new DocumentrException(String.format("Could not determine type %s", type));
226247
}
@@ -258,17 +279,20 @@ public void generate() throws DocumentrException {
258279
}
259280

260281
// now we need to go through and generate the links to the headers
261-
262-
Iterator<Integer> iterator = HEADER_LOOKUP.keySet().iterator();
263-
int start = 0;
264-
StringBuilder renderedStringBuilder = new StringBuilder();
265-
266-
while (iterator.hasNext()) {
267-
Integer headerStart = (Integer) iterator.next();
268-
Integer headerNum = HEADER_LOOKUP.get(headerStart);
269-
renderedStringBuilder.append(Arrays.copyOfRange(charArray, start, headerStart));
270-
renderedStringBuilder.append("\n\n<a name=\"heading_" + headerNum + "\"></a>\n\n");
271-
start = headerStart;
282+
if(hasTocLinks) {
283+
Iterator<Integer> iterator = HEADER_LOOKUP.keySet().iterator();
284+
int start = 0;
285+
StringBuilder renderedStringBuilder = new StringBuilder();
286+
287+
while (iterator.hasNext()) {
288+
Integer headerStart = (Integer) iterator.next();
289+
Integer headerNum = HEADER_LOOKUP.get(headerStart);
290+
renderedStringBuilder.append(Arrays.copyOfRange(charArray, start, headerStart));
291+
renderedStringBuilder.append("\n\n<a name=\"heading_" + headerNum + "\"></a>\n\n");
292+
start = headerStart;
293+
}
294+
295+
rendered = renderedStringBuilder.toString();
272296
}
273297

274298
String markdownToHtml = pegDownProcessor.markdownToHtml(rendered);
@@ -278,14 +302,18 @@ public void generate() throws DocumentrException {
278302
Elements headings = document.select("h1, h2, h3, h4, h5, h6");
279303
for (Element heading : headings) {
280304
int valueOf = Integer.parseInt(heading.nodeName().substring(1));
281-
if(valueOf <= tocLevel)
282-
headerStringBuilder.append( SPACING_LOOKUP.get(valueOf) + "[" + heading.text() + "](#heading_" + numHeader + ")\n");
305+
if(valueOf <= tocLevel) {
306+
if(hasTocLinks) {
307+
headerStringBuilder.append(SPACING_LOOKUP.get(valueOf) + "[" + heading.text() + "](#heading_" + numHeader + ")\n");
308+
} else {
309+
headerStringBuilder.append(SPACING_LOOKUP.get(valueOf) + heading.text() + "\n");
310+
}
311+
}
283312
numHeader++;
284313
}
285314

286315
headerStringBuilder.append("\n\n");
287316

288-
rendered = renderedStringBuilder.toString();
289317
rendered = rendered.replace("§§TABLE_OF_CONTENTS§§", headerStringBuilder.toString());
290318
}
291319

src/main/resources/HELP.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ be one of the following:
3030
- file - the file will be included as is with no parsing done on it
3131
- markup - any valid markdown, with '\n' being replaced with a new line
3232
character. No templar parsing is done on this.
33+
- markdown - an alias for markup
3334
- inbuilt - one of the in-built templates (see below for a list of the
3435
inbuilt templates).
3536
- toc - build up a table of contents, the value set the level of headers that
3637
will be out put. e.g.: value="1" only h1, value="2" only h1 and h2.
38+
- toclinks - whether to display the table of contents as links, and insert
39+
links into the headers (default false)
40+
- backtotop - this will generate a back to top link for each of the
41+
headers (e.g. h1, h2, h3 etc), the value is the text link (default
42+
'top')
43+
3744

3845
The list of inbuilt templates:
3946

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
#
2+
# Implementation class for the plugin
3+
#
14
implementation-class=synapticloop.documentr.plugin.DocumentrPlugin

0 commit comments

Comments
 (0)