-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MarkdownParser: add formatter using mdoc parser
- Loading branch information
Showing
7 changed files
with
46 additions
and
143 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
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
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
119 changes: 0 additions & 119 deletions
119
scalafmt-core/shared/src/main/scala/org/scalafmt/util/MarkdownFile.scala
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
scalafmt-core/shared/src/main/scala/org/scalafmt/util/MarkdownParser.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,36 @@ | ||
package org.scalafmt.util | ||
|
||
import scala.util.Success | ||
import scala.util.Try | ||
|
||
import mdoc.parser._ | ||
|
||
private[scalafmt] object MarkdownParser { | ||
|
||
private val settings: ParserSettings = new ParserSettings { | ||
override val allowCodeFenceIndented: Boolean = true | ||
} | ||
|
||
def transformMdoc(code: String)(fmt: String => Try[String]): Try[String] = { | ||
var hadFencedParts = false | ||
val parts = MarkdownPart.parse(code, settings) | ||
parts.foreach { | ||
case p: CodeFence if p.getMdocMode.isDefined => | ||
fmt(p.body.value) match { | ||
case Success(b) => hadFencedParts = true; p.newBody = Some(b.trim) | ||
case failure => return failure // RETURNING! | ||
} | ||
case _ => | ||
} | ||
|
||
Success { | ||
if (hadFencedParts) { | ||
val out = new StringBuilder() | ||
parts.foreach(_.renderToString(out)) | ||
if (out.last != '\n') out.append('\n') | ||
out.toString() | ||
} else code | ||
} | ||
} | ||
|
||
} |
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
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