Skip to content

Commit b503dfb

Browse files
author
mikemckiernan
authored
feat: use first para as abstract (#607)
Because writers update most ADOC files and specify the first para as the abstract, just use the first para as the abstract if it is not explicitly specified. Update: revert change to ModuleListingServlet.java and use Java 8.
1 parent 56ad1a7 commit b503dfb

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ sling-org-apache-sling-karaf-configs/.settings
2121

2222
pantheon-karaf-dist/src/main/resources/etc/keystore/
2323
pantheon-karaf-dist/src/main/resources/etc/org.ops4j.pax.web.cfg
24+
.m2

pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/extension/MetadataExtractorTreeProcessor.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,23 @@ private void extractAbstract(List<StructuralNode> allNodes) {
122122
}
123123
})
124124
.findFirst();
125+
126+
// if no abstract was specified in the source, use the first paragraph
127+
// as long as the file has more than one block
128+
if (!abstractNode.isPresent() && allNodes.size() > 1) {
129+
abstractNode = allNodes.stream().filter(block -> {
130+
try {
131+
return block.getContext().equals("paragraph");
132+
} catch (Exception e) {
133+
return false;
134+
}
135+
}).findFirst();
136+
}
137+
125138
abstractNode.ifPresent(node -> documentMetadata.mAbstract().set(node.getContent().toString()));
126139

127140
// if no abstract is detected, reset if
128-
if(!abstractNode.isPresent()) {
141+
if (!abstractNode.isPresent()) {
129142
documentMetadata.mAbstract().set(null);
130143
}
131144
}

pantheon-bundle/src/test/java/com/redhat/pantheon/asciidoctor/extension/ModuleMetadataExtractorTreeProcessorTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ void extractMetadataAbstractNotPresent() {
7272
assertNull(metadata.mAbstract().get());
7373
}
7474

75+
@Test
76+
void extractMetadataAbstractInferred() {
77+
// Given
78+
slingContext.build().resource("/content/module1/locales/en_US/1/metadata").commit();
79+
ModuleMetadata metadata =
80+
SlingModels.getModel(
81+
slingContext.resourceResolver().getResource(
82+
"/content/module1/locales/en_US/1/metadata"),
83+
ModuleMetadata.class);
84+
Resource module = slingContext.resourceResolver().getResource("/content/module1");
85+
MetadataExtractorTreeProcessor extension = new MetadataExtractorTreeProcessor(metadata);
86+
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
87+
asciidoctor.javaExtensionRegistry().treeprocessor(extension);
88+
final String adocContent = "// Some comment.\n"
89+
+ "\n"
90+
+ "[id='something_{context}']\n"
91+
+ "= A title for content\n"
92+
+ "\n"
93+
+ "This para is selected as the abstract even if the role is not set.\n"
94+
+ "\n"
95+
+ "Some other text or block like a procedure.";
96+
97+
// When
98+
asciidoctor.load(adocContent, new HashMap<>());
99+
100+
// Then
101+
assertEquals("A title for content", metadata.title().get());
102+
assertEquals("This para is selected as the abstract even if the role is not set.",
103+
metadata.mAbstract().get());
104+
}
105+
75106
@Test
76107
void extractHeadline() {
77108
// Given

0 commit comments

Comments
 (0)