Skip to content

Commit cbd8b3d

Browse files
committed
Merge pull request #19 from kounoike/pr-include
2 parents 687ec6d + 54e02ca commit cbd8b3d

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ jdk:
88
scala:
99
- 2.12.6
1010

11-
script: make package
11+
script: make travis

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
.PHONY: help # List of targets with descriptions
2+
help:
3+
@grep '^\.PHONY: .* #' Makefile | sed 's/\.PHONY: \(.*\) # \(.*\)/\1\t\2/' | expand -t20
4+
15
.PHONY: clean # Clean output files (target dir)
26
clean:
37
rm -rf target/
48

5-
.PHONY: package # Build the project and the plugin package
9+
.PHONY: build # Build the project and the plugin package
610
build:
711
sbt assembly
812

13+
.PHONY: travis # Build on travis
14+
travis: build
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package tobiasroeser.gitbucket.asciidoctor
2+
3+
import java.io.File
4+
import java.net.URI
5+
import java.util
6+
7+
import gitbucket.core.service.{AccountService, RepositoryService}
8+
import gitbucket.core.service.RepositoryService.RepositoryInfo
9+
import gitbucket.core.util.{JGitUtil, StringUtil}
10+
import gitbucket.core.util.Directory._
11+
import gitbucket.core.util.SyntaxSugars._
12+
import org.asciidoctor.ast.DocumentRuby
13+
import org.asciidoctor.extension.{IncludeProcessor, PreprocessorReader}
14+
import org.eclipse.jgit.api.Git
15+
import org.slf4j.LoggerFactory
16+
17+
import scala.collection.JavaConverters._
18+
19+
class AsciidoctorJgitIncludeProcessor(config: java.util.Map[String, Object]) extends IncludeProcessor(config)
20+
with RepositoryService with AccountService{
21+
val logger = LoggerFactory.getLogger(getClass)
22+
23+
override def handles(target: String): Boolean = {
24+
true
25+
}
26+
27+
override def process(document: DocumentRuby, reader: PreprocessorReader, target: String, attributes: util.Map[String, AnyRef]): Unit = {
28+
val documentPath = URI.create(document.getAttr("gitbucket-path").toString)
29+
val repository = document.getAttr("gitbucket-repository").asInstanceOf[RepositoryInfo]
30+
val branch = document.getAttr("gitbucket-branch").toString
31+
val targetPath = documentPath.resolve(target)
32+
33+
using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
34+
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch))
35+
JGitUtil.getContentFromPath(git, revCommit.getTree, targetPath.toString, true).map{ bytes =>
36+
val content = StringUtil.convertFromByteArray(bytes)
37+
val embed = if(attributes.asScala.contains("lines")){
38+
val lines = attributes.get("lines").toString
39+
val linesRe = """(\d+)\.\.(\d+)""".r
40+
lines match {
41+
case linesRe(start, end) =>
42+
content.split("""\r?\n""").slice(start.toInt - 1, end.toInt).mkString("\n")
43+
}
44+
}else{
45+
content
46+
}
47+
reader.push_include(embed, target, target, 1, attributes)
48+
}
49+
}
50+
}
51+
}

src/main/scala/tobiasroeser/gitbucket/asciidoctor/AsciidoctorRenderer.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AsciidoctorRenderer extends Renderer {
3232
case None =>
3333
log.info("About to create Asciidoctor")
3434
_asciidoctor = Option(Asciidoctor.Factory.create(getClass().getClassLoader()))
35+
_asciidoctor.get.javaExtensionRegistry().includeProcessor(classOf[AsciidoctorJgitIncludeProcessor])
3536
_asciidoctor.get
3637
case Some(a) => a
3738
}
@@ -58,6 +59,8 @@ class AsciidoctorRenderer extends Renderer {
5859
attributes.attribute("env-gitbucket", true)
5960
attributes.attribute("outfilesuffix", ".adoc")
6061
attributes.attribute("gitbucket-branch", branch)
62+
attributes.attribute("gitbucket-repository", repository)
63+
attributes.attribute("gitbucket-path", filePath.mkString("/"))
6164

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

0 commit comments

Comments
 (0)