-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kounoike/pr-include
- Loading branch information
Showing
4 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ jdk: | |
scala: | ||
- 2.12.6 | ||
|
||
script: make package | ||
script: make travis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
51 changes: 51 additions & 0 deletions
51
src/main/scala/tobiasroeser/gitbucket/asciidoctor/AsciidoctorJgitIncludeProcessor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters