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

Define an API for mdoc #536

Open
noelwelsh opened this issue Jul 24, 2021 · 1 comment
Open

Define an API for mdoc #536

noelwelsh opened this issue Jul 24, 2021 · 1 comment

Comments

@noelwelsh
Copy link

It would be useful to be able to run mdoc on source that is not in markdown files. The most general way to handle this would be to define an API for programmatically interacting with mdoc. E.g. I would like to be able to define a context containing some imports and then pass strings of code + modifiers and get strings of code + output back. I imagine this already exists internally and it would mostly be a matter of making it public.

@olafurpg
Copy link
Member

olafurpg commented Jul 25, 2021

Thank you for reporting! Have you tried using the current APIs? Mdoc has several classes/methods you might be interested in

  • the worksheet API at https://github.com/scalameta/mdoc/blob/5eaa1149c314d88a4e6809f3e97e538eb4dccdce/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java The official, non-internal, way to instantiate that class is via service providers (which is what Metals does) but you can also do it via new mdoc.internal.worksheets.Mdoc(). The worksheet API gives you full control over the rendering of the evaluated output.
  • the cli API at mdoc.Main.process(mdoc.MainSettings()). You tweak the parameters via the MainSettings builder API. The cli API assumes that you have an input directory with *.md files so it may not be exactly what you're looking for, but you may be able to hack it somehow to support non-markdown files by programmatically creating a temporary directory with your inputs and then post-processing the output
    def withArgs(args: List[String]): MainSettings = {
    if (args.isEmpty) this
    else {
    Settings.fromCliArgs(args, settings) match {
    case Configured.Ok(newSettings) =>
    copy(settings = newSettings)
    case Configured.NotOk(error) =>
    throw new IllegalArgumentException(error.toString())
    }
    }
    }
    def withExcludePath(excludePath: List[PathMatcher]): MainSettings = {
    copy(settings.copy(exclude = excludePath))
    }
    def withIncludePath(includePath: List[PathMatcher]): MainSettings = {
    copy(settings.copy(include = includePath))
    }
    def withSiteVariables(variables: Map[String, String]): MainSettings = {
    copy(settings.copy(site = variables))
    }
    def withWorkingDirectory(cwd: Path): MainSettings = {
    copy(settings.withWorkingDirectory(AbsolutePath(cwd)))
    }
    def withOut(out: Path): MainSettings = {
    withOutputPaths(List(out))
    }
    def withOutputPaths(out: List[Path]): MainSettings = {
    copy(settings.copy(out = out.map(AbsolutePath(_)(settings.cwd))))
    }
    def withIn(in: Path): MainSettings = {
    withInputPaths(List(in))
    }
    def withInputPaths(in: List[Path]): MainSettings = {
    copy(settings.copy(in = in.map(AbsolutePath(_)(settings.cwd))))
    }
    def withClasspath(classpath: String): MainSettings = {
    copy(settings.copy(classpath = classpath))
    }
    def withScalacOptions(scalacOptions: String): MainSettings = {
    copy(settings.copy(scalacOptions = scalacOptions))
    }
    def withStringModifiers(modifiers: List[StringModifier]): MainSettings = {
    copy(settings.copy(stringModifiers = modifiers))
    }
    def withCleanTarget(cleanTarget: Boolean): MainSettings = {
    copy(settings.copy(cleanTarget = cleanTarget))
    }
    def withWatch(watch: Boolean): MainSettings = {
    copy(settings.copy(watch = watch))
    }
    @deprecated("Use withCheck instead", "0.4.1")
    def withTest(test: Boolean): MainSettings = {
    copy(settings.copy(check = test))
    }
    def withCheck(check: Boolean): MainSettings = {
    copy(settings.copy(check = check))
    }
    def withNoLinkHygiene(noLinkHygiene: Boolean): MainSettings = {
    copy(settings.copy(noLinkHygiene = noLinkHygiene))
    }
    def withReportRelativePaths(reportRelativePaths: Boolean): MainSettings = {
    copy(settings.copy(reportRelativePaths = reportRelativePaths))
    }
    def withCharset(charset: Charset): MainSettings = {
    copy(settings.copy(charset = charset))
    }
    def withReporter(reporter: Reporter): MainSettings = {
    copy(reporter = reporter)
    }
    def withInputStream(inputStream: InputStream): MainSettings = {
    copy(settings.copy(inputStream = inputStream))
    }
    def withHeaderIdGenerator(headerIdGenerator: String => String): MainSettings = {
    copy(settings.copy(headerIdGenerator = headerIdGenerator))
    }
    def withVariablePrinter(variablePrinter: Variable => String): MainSettings = {
    copy(settings.copy(variablePrinter = variablePrinter))
    }
    def withCoursierLogger(logger: coursierapi.Logger): MainSettings = {
    copy(settings.copy(coursierLogger = logger))
    }
    def withScreenWidth(screenWidth: Int): MainSettings = {
    copy(settings.copy(screenWidth = screenWidth))
    }
    def withScreenHeight(screenHeight: Int): MainSettings = {
    copy(settings.copy(screenHeight = screenHeight))
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants