diff options
Diffstat (limited to 'doc-tool/src/dotty/tools/dottydoc/staticsite/Template.scala')
-rw-r--r-- | doc-tool/src/dotty/tools/dottydoc/staticsite/Template.scala | 72 |
1 files changed, 65 insertions, 7 deletions
diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Template.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Template.scala index 00d51f083..9720084f3 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Template.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Template.scala @@ -3,7 +3,11 @@ package dottydoc package staticsite import scala.util.control.NonFatal + import dotc.util.SourceFile +import dotc.core.Contexts.Context +import dotc.util.Positions.{ Position, NoPosition } +import util.syntax._ trait Template { def path: String @@ -18,8 +22,9 @@ case class Layout(path: String, content: SourceFile) extends Template case class Include(path: String, content: SourceFile) extends Template -case class LiquidTemplate(path: String, content: String) extends ResourceFinder { +case class LiquidTemplate(path: String, content: SourceFile) extends Template with ResourceFinder { import scala.collection.JavaConverters._ + import dotc.printing.Highlighting._ import liqp.Template import liqp.filters.Filter import liqp.parser.Flavor.JEKYLL @@ -38,16 +43,69 @@ case class LiquidTemplate(path: String, content: String) extends ResourceFinder map } - def render(params: Map[String, AnyRef], includes: Map[String, Include]): String = - try { - Template.parse(content, JEKYLL) + private def protectedRender(op: => String)(implicit ctx: Context) = try { + Some(op) + } catch { + case NonFatal(ex) => { + // TODO: when we reimplement the liquid parser, this can go away. For now + // this is an OK approximation of what went wrong. + if ((ex.getCause eq null) || ex.getMessage.contains("exceeded the max amount of time")) { + ctx.docbase.error( + "unknown error occurred in " + + Blue(path).toString + + ", most likely incorrect usage of tag" + ) + None + } + else ex.getCause match { + case mm: org.antlr.runtime.MismatchedTokenException => { + val unexpected = LiquidTemplate.token(mm.getUnexpectedType) + val expected = LiquidTemplate.token(mm.expecting) + + ctx.error( + if (unexpected == "EOF") + s"unexpected end of file, expected: '$expected'" + else + s"unexpected token '$unexpected', expected: '$expected'", + content atPos Position(mm.index) + ) + + None + } + case ex => { + if (true || ctx.settings.debug.value) + throw ex + + None + } + } + } + } + + def render(params: Map[String, AnyRef], includes: Map[String, Include])(implicit ctx: Context): Option[String] = + protectedRender { + Template.parse(show, JEKYLL) .`with`(ResourceInclude(params, includes)) .`with`(RenderReference(params)) .`with`(RenderTitle(params)) .`with`(Docstring(params)) .render(toJavaMap(params)) - } catch { - case NonFatal(ex) => - throw TemplateRenderingError(path, ex) } } + +object LiquidTemplate { + import liqp.parser.LiquidParser + + private val _tokens: Map[String, String] = Map( + "TagStart" -> "{%", + "TagEnd" -> "%}" + ) + + def token(i: Int): String = + if (i == -1) "EOF" + else if (i >= LiquidParser.tokenNames.length) + "non-existing token" + else _tokens + .get(LiquidParser.tokenNames(i)) + .getOrElse(s"token $i") +} |