bjornregnell / taggy

A Scala 3 micro-library for easy slides with Scala's contextual abstraction.
Apache License 2.0
8 stars 1 forks source link

replace URL:s with \url{} #1

Closed bjornregnell closed 2 years ago

bjornregnell commented 2 years ago

copy implementation from https://github.com/bjornregnell/new-in-Scala3/blob/main/api.scala#L116

bjornregnell commented 2 years ago

here is da thing: change lines 112-144 to (note better error messages)

  def urlPattern = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]".r

  val replacePatterns = Map[util.matching.Regex, (String, String, Int)](
    beginEndPattern("\\*\\*") -> ("\\\\textbf{", "}", 1),
    beginEndPattern("\\*")    -> ("\\\\textit{", "}", 1),
    beginEndPattern("\\`")    -> ("\\\\texttt{", "}", 1),
    urlPattern                -> ("{\\\\footnotesize{\\\\url{", "}}}", 0)
  )

  extension (s: String) 
    def replaceAllMarkers: String = 
      var result = s
      for (pattern, (b, e, i)) <- replacePatterns do
        result = pattern.replaceAllIn(result, m => s"$b${m.group(i)}$e")
      result

    def minimizeMargin: String = 
      val xs = s.split("\n")
      val minMargin = xs.filter(_.nonEmpty).map(_.takeWhile(_.isWhitespace).length).minOption.getOrElse(0)
      xs.map(_.drop(minMargin)).mkString("\n")
  end extension

  def make(tree: Tree, out: String, workDir: String)(using pre: Preamble): Int = 
    import scala.sys.process.{Process  => OSProc}               // rename import
    createDirs(workDir)
    (pre.value ++ tree.toLatex).saveTo(s"$workDir/$out.tex")
    val wd = java.io.File(workDir)
      val proc = OSProc(Seq("latexmk", "-pdf", "-cd", "-halt-on-error", "-silent", s"$out.tex"), wd)
      val procOutputFile = java.io.File(s"$workDir/$out.log")
      val result = proc.#>(procOutputFile).run.exitValue
      if result == 0 then println(s"Latex output generated in $workDir/$out.pdf")
      else println(s"Latex ERROR in $workDir/$out.log")
      result
bjornregnell commented 2 years ago

check for more diffs with https://github.com/bjornregnell/new-in-Scala3/blob/main/api.scala