Skip to content

Commit

Permalink
feat: use first para as abstract (#607)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikemckiernan committed Jun 14, 2021
1 parent 56ad1a7 commit b503dfb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ sling-org-apache-sling-karaf-configs/.settings

pantheon-karaf-dist/src/main/resources/etc/keystore/
pantheon-karaf-dist/src/main/resources/etc/org.ops4j.pax.web.cfg
.m2
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,23 @@ private void extractAbstract(List<StructuralNode> allNodes) {
}
})
.findFirst();

// if no abstract was specified in the source, use the first paragraph
// as long as the file has more than one block
if (!abstractNode.isPresent() && allNodes.size() > 1) {
abstractNode = allNodes.stream().filter(block -> {
try {
return block.getContext().equals("paragraph");
} catch (Exception e) {
return false;
}
}).findFirst();
}

abstractNode.ifPresent(node -> documentMetadata.mAbstract().set(node.getContent().toString()));

// if no abstract is detected, reset if
if(!abstractNode.isPresent()) {
if (!abstractNode.isPresent()) {
documentMetadata.mAbstract().set(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ void extractMetadataAbstractNotPresent() {
assertNull(metadata.mAbstract().get());
}

@Test
void extractMetadataAbstractInferred() {
// Given
slingContext.build().resource("/content/module1/locales/en_US/1/metadata").commit();
ModuleMetadata metadata =
SlingModels.getModel(
slingContext.resourceResolver().getResource(
"/content/module1/locales/en_US/1/metadata"),
ModuleMetadata.class);
Resource module = slingContext.resourceResolver().getResource("/content/module1");
MetadataExtractorTreeProcessor extension = new MetadataExtractorTreeProcessor(metadata);
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
asciidoctor.javaExtensionRegistry().treeprocessor(extension);
final String adocContent = "// Some comment.\n"
+ "\n"
+ "[id='something_{context}']\n"
+ "= A title for content\n"
+ "\n"
+ "This para is selected as the abstract even if the role is not set.\n"
+ "\n"
+ "Some other text or block like a procedure.";

// When
asciidoctor.load(adocContent, new HashMap<>());

// Then
assertEquals("A title for content", metadata.title().get());
assertEquals("This para is selected as the abstract even if the role is not set.",
metadata.mAbstract().get());
}

@Test
void extractHeadline() {
// Given
Expand Down

0 comments on commit b503dfb

Please sign in to comment.