Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for indented and tagged codefence blocks #599

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/src/main/scala/mdoc/internal/cli/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ case class Settings(
charset: Charset = StandardCharsets.UTF_8,
@Description("The working directory to use for making relative paths absolute.")
cwd: AbsolutePath,
@Description("Allow indented code fence blocks")
allowCodeFenceIndented: Boolean = false,
andyglow marked this conversation as resolved.
Show resolved Hide resolved
@Hidden()
stringModifiers: List[StringModifier] = StringModifier.default(),
@Hidden()
Expand Down
7 changes: 6 additions & 1 deletion mdoc/src/main/scala/mdoc/internal/markdown/Markdown.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ object Markdown {
reporter,
settings
)
val file = MarkdownFile.parse(textWithVariables, inputFile, reporter)
val file = MarkdownFile.parse(
textWithVariables,
inputFile,
reporter,
settings
)
val processor = new Processor()(context)
processor.processDocument(file)
file.renderToString
Expand Down
92 changes: 71 additions & 21 deletions mdoc/src/main/scala/mdoc/internal/markdown/MarkdownFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package mdoc.internal.markdown
import scala.meta.inputs.Position
import scala.meta.inputs.Input
import mdoc.Reporter

import scala.collection.mutable
import scala.meta.io.RelativePath
import mdoc.internal.cli.InputFile
import mdoc.internal.cli.{InputFile, Settings}

final case class MarkdownFile(input: Input, file: InputFile, parts: List[MarkdownPart]) {
private val appends = mutable.ListBuffer.empty[String]
Expand All @@ -20,12 +21,26 @@ final case class MarkdownFile(input: Input, file: InputFile, parts: List[Markdow
}
}
object MarkdownFile {
object syntax {
andyglow marked this conversation as resolved.
Show resolved Hide resolved
private[markdown] implicit class StringOps(private val x: String) extends AnyVal {
def isNL: Boolean = x.forall { c => c == '\n' || c == '\r' }
andyglow marked this conversation as resolved.
Show resolved Hide resolved
}

private[markdown] implicit class StringBuilderOps(private val x: StringBuilder) extends AnyVal {
def appendLinesPrefixed(prefix: String, text: String): Unit = {
text.linesWithSeparators foreach { line =>
if (line.nonEmpty && !line.isNL && !line.startsWith(prefix)) x.append(prefix)
x.append(line)
}
}
}
}
sealed abstract class State
object State {
case class CodeFence(start: Int, backticks: String, info: String) extends State
case class CodeFence(start: Int, backticks: String, info: String, indent: Int) extends State
case object Text extends State
}
class Parser(input: Input, reporter: Reporter) {
class Parser(input: Input, reporter: Reporter, settings: Settings) {
private val text = input.text
private def newPos(start: Int, end: Int): Position = {
Position.Range(input, start, end)
Expand All @@ -41,12 +56,15 @@ object MarkdownFile {
backtickStart: Int,
backtickEnd: Int
): CodeFence = {
val open = newText(state.start, state.start + state.backticks.length())
// tag is characters preceding the code fence in this line
val tag = newText(state.start, state.start + state.indent)
andyglow marked this conversation as resolved.
Show resolved Hide resolved
val open = newText(state.start, state.start + state.indent + state.backticks.length())
.dropLinePrefix(state.indent)
val info = newText(open.pos.end, open.pos.end + state.info.length())
val adaptedBacktickStart = math.max(0, backtickStart - 1)
val body = newText(info.pos.end, adaptedBacktickStart)
val close = newText(adaptedBacktickStart, backtickEnd)
val part = CodeFence(open, info, body, close)
val body = newText(info.pos.end, adaptedBacktickStart).dropLinePrefix(state.indent)
val close = newText(adaptedBacktickStart, backtickEnd).dropLinePrefix(state.indent)
val part = CodeFence(open, info, body, close, tag)
part.pos = newPos(state.start, backtickEnd)
part
}
Expand All @@ -58,16 +76,19 @@ object MarkdownFile {
val end = curr + line.length()
state match {
case State.Text =>
if (line.startsWith("```")) {
val backticks = line.takeWhile(_ == '`')
val info = line.substring(backticks.length())
state = State.CodeFence(curr, backticks, info)
val start = line.indexOf("```")
if (start == 0 || (start > 0 && settings.allowCodeFenceIndented)) {
val fence = line.substring(start)
val backticks = fence.takeWhile(_ == '`')
val info = fence.substring(backticks.length())
state = State.CodeFence(curr, backticks, info, indent = start)
} else {
parts += newText(curr, end)
}
case s: State.CodeFence =>
val start = line.indexOf(s.backticks)
if (
line.startsWith(s.backticks) &&
start == s.indent &&
line.forall(ch => ch == '`' || ch.isWhitespace)
) {
parts += newCodeFence(s, curr, end)
Expand All @@ -84,24 +105,33 @@ object MarkdownFile {
parts.toList
}
}
def parse(input: Input, file: InputFile, reporter: Reporter): MarkdownFile = {
val parser = new Parser(input, reporter)
def parse(
input: Input,
file: InputFile,
reporter: Reporter,
settings: Settings
): MarkdownFile = {
val parser = new Parser(input, reporter, settings)
val parts = parser.acceptParts()
MarkdownFile(input, file, parts)
}
}

sealed abstract class MarkdownPart {
import MarkdownFile.syntax._

var pos: Position = Position.None
final def renderToString(out: StringBuilder): Unit =
this match {
case Text(value) =>
out.append(value)
case fence: CodeFence =>
val indentation = if (fence.hasBlankTag) fence.tag.value else " " * fence.indent
fence.newPart match {
case Some(newPart) =>
out.append(newPart)
out.appendLinesPrefixed(indentation, newPart)
case None =>
fence.tag.renderToString(out)
fence.openBackticks.renderToString(out)
fence.newInfo match {
case None =>
Expand All @@ -114,18 +144,38 @@ sealed abstract class MarkdownPart {
}
fence.newBody match {
case None =>
fence.body.renderToString(out)
out.appendLinesPrefixed(indentation, fence.body.value)
case Some(newBody) =>
out.append(newBody)
out.appendLinesPrefixed(indentation, newBody)
}
fence.closeBackticks.renderToString(out)
out.appendLinesPrefixed(indentation, fence.closeBackticks.value)
}
}
}
final case class Text(value: String) extends MarkdownPart
final case class CodeFence(openBackticks: Text, info: Text, body: Text, closeBackticks: Text)
extends MarkdownPart {
final case class Text(value: String) extends MarkdownPart {
import MarkdownFile.syntax._

def dropLinePrefix(indent: Int): Text = {
if (indent > 0) {
val updatedValue = value.linesWithSeparators.map { line =>
if (!line.isNL && line.length >= indent) line.substring(indent) else line
}.mkString
val updatedText = Text(updatedValue)
updatedText.pos = this.pos
updatedText
} else this
}
}
final case class CodeFence(
openBackticks: Text,
info: Text,
body: Text,
closeBackticks: Text,
tag: Text = Text("")
) extends MarkdownPart {
var newPart = Option.empty[String]
var newInfo = Option.empty[String]
var newBody = Option.empty[String]
def indent: Int = tag.value.length
def hasBlankTag: Boolean = tag.value.forall(_.isWhitespace)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package tests.markdown

class IndentedMarkdownSuite extends BaseMarkdownSuite {

check(
"2 whitespace",
"""
| ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
"""
| ```scala
| val msg = "Hello!"
| println(msg)
andyglow marked this conversation as resolved.
Show resolved Hide resolved
| ```
""".stripMargin,
settings = baseSettings.copy(allowCodeFenceIndented = true)
)

check(
"4 whitespace",
"""
| ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
"""
| ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
settings = baseSettings.copy(allowCodeFenceIndented = true)
)

check(
"tab",
"""
| ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
"""
| ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
settings = baseSettings.copy(allowCodeFenceIndented = true)
)

check(
"4 whitespace, tagged",
"""
|: ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
"""
|: ```scala
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
settings = baseSettings.copy(allowCodeFenceIndented = true)
)

check(
"4 whitespace, tagged, mdoc",
"""
|: ```scala mdoc
| val msg = "Hello!"
| println(msg)
| ```
""".stripMargin,
"""
|: ```scala
| val msg = "Hello!"
| // msg: String = "Hello!"
| println(msg)
| // Hello!
| ```
""".stripMargin,
settings = baseSettings.copy(allowCodeFenceIndented = true)
)
}
Loading