Skip to content

Commit

Permalink
Fixes asciidoctor#1037. Avoid usage of deprecated attributes toc2 and…
Browse files Browse the repository at this point in the history
… toc-placement.
  • Loading branch information
robertpanzer committed May 16, 2021
1 parent 0d1b4c8 commit faf4499
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co

== Unreleased

Bug Fixes::

* Avoid using of deprecated attributes toc2 and toc-placement. Deprecated (@abelsromero) (#1037)

== 2.5.1 (2021-05-04)

Improvement::
Expand Down
11 changes: 8 additions & 3 deletions asciidoctorj-api/src/main/java/org/asciidoctor/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ public void setIgnoreUndefinedAttributes(boolean ignoreUndefinedAttributes) {
*
* @param placement
* where toc is rendered.
* @deprecated Use {@link #setTableOfContents(Placement)}
*/
@Deprecated
public void setTableOfContents2(Placement placement) {
this.attributes.put(TOC_2, toAsciidoctorFlag(true));
this.attributes.put(TOC_POSITION, placement.getPosition());
Expand All @@ -346,8 +348,7 @@ public void setTableOfContents2(Placement placement) {
* position of toc.
*/
public void setTableOfContents(Placement placement) {
this.attributes.put(TOC, toAsciidoctorFlag(true));
this.attributes.put(TOC_POSITION, placement.getPosition());
this.attributes.put(TOC, placement.getPosition());
}

/**
Expand Down Expand Up @@ -383,7 +384,11 @@ public void setShowTitle(boolean showTitle) {
* value.
*/
public void setTableOfContents(boolean toc) {
this.attributes.put(TOC, toAsciidoctorFlag(toc));
if (toc) {
this.attributes.put(TOC, "");
} else {
this.attributes.remove(TOC);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public AttributesBuilder tableOfContents(Placement placement) {
* Sets table of contents 2 attribute.
* @param placement where toc is rendered.
* @return this instance.
* @deprecated Use {@link #tableOfContents(Placement)}
*/
@Deprecated
public AttributesBuilder tableOfContents2(Placement placement) {
this.attributes.setTableOfContents2(placement);
return this;
Expand Down
9 changes: 5 additions & 4 deletions asciidoctorj-api/src/main/java/org/asciidoctor/Placement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

public enum Placement {


TOP("top"), BOTTOM("bottom"), LEFT("left"), RIGHT("right");


TOP("top"), BOTTOM("bottom"), LEFT("left"), RIGHT("right"),
PREAMBLE("preamble"), MACRO("macro");

private String position;

private Placement(String position) {
Placement(String position) {
this.position = position;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
package org.asciidoctor;

import static org.asciidoctor.AttributesBuilder.attributes;
import static org.asciidoctor.OptionsBuilder.options;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.xmlmatchers.xpath.HasXPath.hasXPath;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import com.google.common.io.CharStreams;
import org.asciidoctor.arquillian.api.Unshared;
import org.asciidoctor.util.ClasspathResources;
import org.jboss.arquillian.junit.Arquillian;
Expand All @@ -43,7 +14,28 @@
import org.junit.runner.RunWith;
import org.xml.sax.SAXException;

import com.google.common.io.CharStreams;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import static org.asciidoctor.AttributesBuilder.attributes;
import static org.asciidoctor.OptionsBuilder.options;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
import static org.junit.Assert.assertEquals;
import static org.xmlmatchers.xpath.HasXPath.hasXPath;

@RunWith(Arquillian.class)
public class WhenAttributesAreUsedInAsciidoctor {
Expand Down Expand Up @@ -364,8 +356,8 @@ public void ignore_undefined_attributes_should_keep_lines_with_undefined_attribu
@Test
public void setting_toc_attribute_table_of_contents_should_be_generated() throws IOException {

Attributes attributes = attributes().tableOfContents(true).get();
Options options = options().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).get();
Attributes attributes = Attributes.builder().tableOfContents(true).build();
Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build();

asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options);

Expand All @@ -376,6 +368,58 @@ public void setting_toc_attribute_table_of_contents_should_be_generated() throws

}

@Test
public void setting_toc_attribute_left_should_work() throws IOException {
Attributes attributes = Attributes.builder().tableOfContents(Placement.LEFT).build();
Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build();

asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options);

Document doc = Jsoup.parse(new File(testFolder.getRoot(),
"tocsample.html"), "UTF-8");
Elements tocElement = doc.select("body.toc-left > div#header > div#toc");
assertThat(tocElement.size(), is(1));
}

@Test
public void setting_toc_attribute_right_should_work() throws IOException {
Attributes attributes = Attributes.builder().tableOfContents(Placement.RIGHT).build();
Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build();

asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options);

Document doc = Jsoup.parse(new File(testFolder.getRoot(),
"tocsample.html"), "UTF-8");
Elements tocElement = doc.select("body.toc-right > div#header > div#toc");
assertThat(tocElement.size(), is(1));
}

@Test
public void setting_toc_attribute_preamble_should_work() throws IOException {
Attributes attributes = Attributes.builder().tableOfContents(Placement.PREAMBLE).build();
Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build();

asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options);

Document doc = Jsoup.parse(new File(testFolder.getRoot(),
"tocsample.html"), "UTF-8");
Elements tocElement = doc.select("body.article > div#content > div#preamble > div#toc");
assertThat(tocElement.size(), is(1));
}

@Test
public void setting_toc_attribute_macro_should_work() throws IOException {
Attributes attributes = Attributes.builder().tableOfContents(Placement.MACRO).build();
Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build();

asciidoctor.convertFile(classpath.getResource("tocsamplemacro.asciidoc"), options);

Document doc = Jsoup.parse(new File(testFolder.getRoot(),
"tocsamplemacro.html"), "UTF-8");
Elements tocElement = doc.select("body.article > div#content > div.sect1 > div.sectionbody > div#toc");
assertThat(tocElement.size(), is(1));
}

@Test
public void attribute_missing_should_drop_line_should_drop_line_with_reference_to_missing_attribute_if_attribute_missing_attribute_is_drop_line() {

Expand Down
2 changes: 2 additions & 0 deletions asciidoctorj-core/src/test/resources/tocsample.asciidoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
= Title

The preamble.

== First chapter
first chapter

Expand Down
11 changes: 11 additions & 0 deletions asciidoctorj-core/src/test/resources/tocsamplemacro.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
= Title

The preamble.

== First chapter
first chapter

toc::[]

== Second chapter
second chapter

0 comments on commit faf4499

Please sign in to comment.