Skip to content

Commit

Permalink
Merge pull request #107 from raboof/switch-groups
Browse files Browse the repository at this point in the history
Switching tabs as well as other fragments
  • Loading branch information
pvlugter authored May 12, 2017
2 parents 91f2a8c + 7f55c0c commit f71a5c3
Show file tree
Hide file tree
Showing 28 changed files with 697 additions and 51 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ lazy val docs = (project in file("docs"))
name := "paradox docs",
paradoxTheme := Some(builtinParadoxTheme("generic")),
paradoxProperties in Compile += ("empty" -> "")
//paradoxGroups := Map("Languages" -> Seq("Scala", "Java"))
)

addCommandAlias("verify", ";test ;scripted ;docs/paradox")
Expand Down
43 changes: 43 additions & 0 deletions core/src/main/java/org/pegdown/ast/VerbatimGroupNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.pegdown.ast;

public class VerbatimGroupNode extends VerbatimNode {
private final String group;

public VerbatimGroupNode(String text) {
this(text, "", "");
}

public VerbatimGroupNode(String text, String type) {
this(text, type, "");
}

public VerbatimGroupNode(String text, String type, String group) {
super(text, type);
this.group = group;
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

public String getGroup() {
return group;
}
}
12 changes: 8 additions & 4 deletions core/src/main/scala/com/lightbend/paradox/ParadoxProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.lightbend.paradox

import com.lightbend.paradox.markdown.{ Breadcrumbs, Page, Path, Reader, TableOfContents, Writer, Frontin, PropertyUrl, Url }
import com.lightbend.paradox.template.PageTemplate
import com.lightbend.paradox.markdown.{ Breadcrumbs, Groups, Page, Path, Reader, TableOfContents, Writer, Frontin, PropertyUrl, Url }
import com.lightbend.paradox.tree.Tree.{ Forest, Location }
import java.io.File
import org.pegdown.ast.{ ActiveLinkNode, ExpLinkNode, RootNode }
Expand All @@ -38,10 +38,13 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
outputDirectory: File,
sourceSuffix: String,
targetSuffix: String,
groups: Map[String, Seq[String]],
properties: Map[String, String],
navigationDepth: Int,
pageTemplate: PageTemplate,
errorListener: STErrorListener): Seq[(File, String)] = {
require(!groups.values.flatten.map(_.toLowerCase).groupBy(identity).values.exists(_.size > 1), "Group names may not overlap")

val pages = parsePages(mappings, Path.replaceSuffix(sourceSuffix, targetSuffix))
val paths = Page.allPaths(pages).toSet
val globalPageMappings = rootPageMappings(pages)
Expand All @@ -52,10 +55,10 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
val page = loc.tree.label
val pageProperties = properties ++ page.properties.get
val currentMapping = Path.generateTargetFile(Path.relativeLocalPath(page.rootSrcPage, page.file.getPath), globalPageMappings)_
val writerContext = Writer.Context(loc, paths, currentMapping, sourceSuffix, targetSuffix, pageProperties)
val writerContext = Writer.Context(loc, paths, currentMapping, sourceSuffix, targetSuffix, groups, pageProperties)
val pageToc = new TableOfContents(pages = true, headers = false, ordered = false, maxDepth = navigationDepth)
val headerToc = new TableOfContents(pages = false, headers = true, ordered = false, maxDepth = navigationDepth)
val pageContext = PageContents(leadingBreadcrumbs, loc, writer, writerContext, pageToc, headerToc)
val pageContext = PageContents(leadingBreadcrumbs, groups, loc, writer, writerContext, pageToc, headerToc)
val outputFile = new File(outputDirectory, page.path)
outputFile.getParentFile.mkdirs
pageTemplate.write(page.properties(Page.Properties.DefaultLayoutMdIndicator, pageTemplate.defaultName), pageContext, outputFile, errorListener)
Expand All @@ -68,7 +71,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
/**
* Default template contents for a markdown page at a particular location.
*/
case class PageContents(leadingBreadcrumbs: List[(String, String)], loc: Location[Page], writer: Writer, context: Writer.Context, pageToc: TableOfContents, headerToc: TableOfContents) extends PageTemplate.Contents {
case class PageContents(leadingBreadcrumbs: List[(String, String)], groups: Map[String, Seq[String]], loc: Location[Page], writer: Writer, context: Writer.Context, pageToc: TableOfContents, headerToc: TableOfContents) extends PageTemplate.Contents {
import scala.collection.JavaConverters._

private val page = loc.tree.label
Expand All @@ -83,6 +86,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
lazy val getNext = link(loc.next)
lazy val getBreadcrumbs = writer.writeBreadcrumbs(Breadcrumbs.markdown(leadingBreadcrumbs, loc.path), context)
lazy val getNavigation = writer.writeNavigation(pageToc.root(loc), context)
lazy val getGroups = Groups.html(groups)
lazy val hasSubheaders = page.headers.nonEmpty
lazy val getToc = writer.writeToc(headerToc.headers(loc), context)
lazy val getSource_url = githubLink(Some(loc)).getHtml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ case class SnipDirective(page: Page, variables: Map[String, String])
} else new File(page.file.getParentFile, source)
val text = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file))
new VerbatimNode(text, lang).accept(visitor)
val group = Option(node.attributes.value("group")).getOrElse("")
new VerbatimGroupNode(text, lang, group).accept(visitor)
} catch {
case e: FileNotFoundException =>
throw new SnipDirective.LinkException(s"Unknown snippet [${e.getMessage}] referenced from [${page.path}]")
Expand Down Expand Up @@ -473,3 +474,34 @@ case class WrapDirective(typ: String) extends ContainerBlockDirective(Array(typ,
printer.print(s"</$typ>")
}
}

/**
* Inline wrap directive
*
* Wraps inner contents in a `span`, optionally with custom `id` and/or `class` attributes.
*/
case class InlineWrapDirective(typ: String) extends InlineDirective("span") {
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
val id =
node.attributes.identifier match {
case null => ""
case x => s""" id="$x""""
}
val classes =
node.attributes.classesString match {
case "" => ""
case x => s""" class="$x""""
}
printer.print(s"""<$typ$id$classes>""")
node.contentsNode.accept(visitor)
printer.print(s"</$typ>")
}
}

case class InlineGroupDirective(groups: Seq[String]) extends InlineDirective(groups: _*) {
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
printer.print(s"""<span class="group-${node.name}">""")
node.contentsNode.accept(visitor)
printer.print(s"</span>")
}
}
28 changes: 28 additions & 0 deletions core/src/main/scala/com/lightbend/paradox/markdown/Groups.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.lightbend.paradox.markdown

object Groups {
def html(supergroups: Map[String, Seq[String]]) = {
supergroups.map {
case (supergroup, groups) =>
s"""<select class="supergroup" name="$supergroup">""" +
groups.map(group => s"""<option class="group" value="group-${group.toLowerCase}">$group</option>""").mkString +
"</select>"
}.mkString("\n")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
package com.lightbend.paradox.markdown

import org.parboiled.common.StringUtils
import org.pegdown.ast.VerbatimNode
import org.pegdown.ast.{ VerbatimNode, VerbatimGroupNode }
import org.pegdown.{ Printer, VerbatimSerializer }

/**
* Add markup around verbatim blocks.
*/
abstract class StyledVerbatimSerializer extends VerbatimSerializer {

def printPreAttributes(printer: Printer): Unit
def printPreAttributes(printer: Printer, nodeGroup: String = ""): Unit

def printCodeAttributes(printer: Printer, nodeType: String): Unit

Expand All @@ -34,7 +34,10 @@ abstract class StyledVerbatimSerializer extends VerbatimSerializer {

printer.print("<pre")
if (!StringUtils.isEmpty(node.getType)) {
printPreAttributes(printer)
node match {
case vgn: VerbatimGroupNode => printPreAttributes(printer, vgn.getGroup)
case vn: VerbatimNode => printPreAttributes(printer)
}
}
printer.print(">")

Expand Down Expand Up @@ -66,7 +69,13 @@ abstract class StyledVerbatimSerializer extends VerbatimSerializer {
* Add prettify markup around verbatim blocks.
*/
object PrettifyVerbatimSerializer extends StyledVerbatimSerializer {
override def printPreAttributes(printer: Printer): Unit = printClass(printer, "prettyprint")
override def printPreAttributes(printer: Printer, nodeGroup: String): Unit = {
nodeGroup match {
case "" => printClass(printer, "prettyprint")
case g => printClass(printer, "prettyprint group-" + g)
}
}

override def printCodeAttributes(printer: Printer, nodeType: String): Unit = nodeType match {
case "text" | "nocode" => printClass(printer, "nocode")
case _ => printClass(printer, s"language-$nodeType")
Expand Down
20 changes: 15 additions & 5 deletions core/src/main/scala/com/lightbend/paradox/markdown/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Writer(serializer: Writer.Context => ToHtmlSerializer) {
def writeNavigation(node: Node, context: Writer.Context): String =
writeFragment(node, context)

/**
* Write groups fragment.
*/
def writeGroups(node: Node, context: Writer.Context): String =
writeFragment(node, context)

/**
* Write navigation fragment.
*/
Expand Down Expand Up @@ -89,10 +95,11 @@ object Writer {
case class Context(
location: Location[Page],
paths: Set[String],
pageMappings: String => String = Path.replaceExtension(DefaultSourceSuffix, DefaultTargetSuffix),
sourceSuffix: String = DefaultSourceSuffix,
targetSuffix: String = DefaultTargetSuffix,
properties: Map[String, String] = Map.empty)
pageMappings: String => String = Path.replaceExtension(DefaultSourceSuffix, DefaultTargetSuffix),
sourceSuffix: String = DefaultSourceSuffix,
targetSuffix: String = DefaultTargetSuffix,
groups: Map[String, Seq[String]] = Map.empty,
properties: Map[String, String] = Map.empty)

def defaultLinks(context: Context): LinkRenderer =
new DefaultLinkRenderer(context)
Expand Down Expand Up @@ -120,7 +127,10 @@ object Writer {
VarsDirective(context.properties),
CalloutDirective("note", "Note"),
CalloutDirective("warning", "Warning"),
WrapDirective("div"))
WrapDirective("div"),
InlineWrapDirective("span"),
InlineGroupDirective(context.groups.values.flatten.map(_.toLowerCase).toSeq)
)

class DefaultLinkRenderer(context: Context) extends LinkRenderer {
private lazy val imgBase = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.lightbend.paradox.markdown

import com.lightbend.paradox.tree.Tree.Location

class InlineGroupDirectiveSpec extends MarkdownBaseSpec {

"The inline `group` directive" should "render wrapping groups" in {
markdown("@scala[Simple sentence here]") shouldEqual html("""<p><span class="group-scala">Simple sentence here</span></p>""")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.lightbend.paradox.markdown

import com.lightbend.paradox.tree.Tree.Location

class InlineWrapDirectiveSpec extends MarkdownBaseSpec {

"The inline `wrap` directive" should "render wrapping `span`s" in {
markdown("@span[Simple sentence here]") shouldEqual html("<p><span>Simple sentence here</span></p>")
}

it should "render the example from the docs" in {
markdown("@span[Scala variant containing ***markdown*** and @ref:[Linking](test.md)] { .group-scala }") shouldEqual html("""
|<p><span class="group-scala">Scala variant containing <strong><em>markdown</em></strong> and <a href="test.html">Linking</a></span></p>""")
}

it should "support a custom id and custom CSS classes at the same time" in {
markdown("@span[Simple sentence here.] { #yeah .red .blue }") shouldEqual html("""<p><span id="yeah" class="red blue">Simple sentence here.</div></p>""")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ abstract class MarkdownBaseSpec extends FlatSpec with Matchers {
}

def writerContext(location: Location[Page]): Writer.Context = {
Writer.Context(location, Page.allPaths(List(location.root.tree)).toSet)
Writer.Context(
location,
Page.allPaths(List(location.root.tree)).toSet,
groups = Map("Language" -> Seq("Scala", "Java"))
)
}

def pages(mappings: (String, String)*): Forest[Page] = {
Expand Down
Loading

0 comments on commit f71a5c3

Please sign in to comment.