Skip to content

Commit

Permalink
Merge pull request #19 from kounoike/pr-include
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Jul 17, 2018
2 parents 687ec6d + 54e02ca commit cbd8b3d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ jdk:
scala:
- 2.12.6

script: make package
script: make travis
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
.PHONY: help # List of targets with descriptions
help:
@grep '^\.PHONY: .* #' Makefile | sed 's/\.PHONY: \(.*\) # \(.*\)/\1\t\2/' | expand -t20

.PHONY: clean # Clean output files (target dir)
clean:
rm -rf target/

.PHONY: package # Build the project and the plugin package
.PHONY: build # Build the project and the plugin package
build:
sbt assembly

.PHONY: travis # Build on travis
travis: build
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tobiasroeser.gitbucket.asciidoctor

import java.io.File
import java.net.URI
import java.util

import gitbucket.core.service.{AccountService, RepositoryService}
import gitbucket.core.service.RepositoryService.RepositoryInfo
import gitbucket.core.util.{JGitUtil, StringUtil}
import gitbucket.core.util.Directory._
import gitbucket.core.util.SyntaxSugars._
import org.asciidoctor.ast.DocumentRuby
import org.asciidoctor.extension.{IncludeProcessor, PreprocessorReader}
import org.eclipse.jgit.api.Git
import org.slf4j.LoggerFactory

import scala.collection.JavaConverters._

class AsciidoctorJgitIncludeProcessor(config: java.util.Map[String, Object]) extends IncludeProcessor(config)
with RepositoryService with AccountService{
val logger = LoggerFactory.getLogger(getClass)

override def handles(target: String): Boolean = {
true
}

override def process(document: DocumentRuby, reader: PreprocessorReader, target: String, attributes: util.Map[String, AnyRef]): Unit = {
val documentPath = URI.create(document.getAttr("gitbucket-path").toString)
val repository = document.getAttr("gitbucket-repository").asInstanceOf[RepositoryInfo]
val branch = document.getAttr("gitbucket-branch").toString
val targetPath = documentPath.resolve(target)

using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch))
JGitUtil.getContentFromPath(git, revCommit.getTree, targetPath.toString, true).map{ bytes =>
val content = StringUtil.convertFromByteArray(bytes)
val embed = if(attributes.asScala.contains("lines")){
val lines = attributes.get("lines").toString
val linesRe = """(\d+)\.\.(\d+)""".r
lines match {
case linesRe(start, end) =>
content.split("""\r?\n""").slice(start.toInt - 1, end.toInt).mkString("\n")
}
}else{
content
}
reader.push_include(embed, target, target, 1, attributes)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AsciidoctorRenderer extends Renderer {
case None =>
log.info("About to create Asciidoctor")
_asciidoctor = Option(Asciidoctor.Factory.create(getClass().getClassLoader()))
_asciidoctor.get.javaExtensionRegistry().includeProcessor(classOf[AsciidoctorJgitIncludeProcessor])
_asciidoctor.get
case Some(a) => a
}
Expand All @@ -58,6 +59,8 @@ class AsciidoctorRenderer extends Renderer {
attributes.attribute("env-gitbucket", true)
attributes.attribute("outfilesuffix", ".adoc")
attributes.attribute("gitbucket-branch", branch)
attributes.attribute("gitbucket-repository", repository)
attributes.attribute("gitbucket-path", filePath.mkString("/"))

val asciidocAttributes = new File(GitBucketHome, "/asciidoctor.properties")
val propsJava = new Properties();
Expand Down

0 comments on commit cbd8b3d

Please sign in to comment.