<commit_before>package backend.engine

import scala.xml._
import play.api.Play

/**
 * A class implementing the compile method of the TextCompiler. The class is NOT thread safe!
 *
 * @author <NAME>
 *
 */
// TODO: Make it thread safe, use more functional style, make it less accepting of misused sbtl, testing...
class TextCompilerSbtl() extends TextCompiler {
  private val blogUrl = Play.current.configuration.getString("blogUrl").getOrElse("http://bleibinha.us/blog")
  private val baseBlogUrl = blogUrl.substring(0, blogUrl.indexOf("/blog"))
  private val externEvidence = "://"
  private val fileHostUrl = blogUrl + "/file/"
  private var footnotes: List[Footnote] = Nil
  private var footnotesNumber: Int = 1
  private var headers: List[List[Header]] = Nil
  private var refs: List[Ref] = Nil
  private var refNumber: Int = 1
  private var images: Int = 1
  private var codeSnippets: Int = 1
  private var internReferences: List[InternReference] = Nil

  /* (non-Javadoc)
 * @see backend.engine.TextCompiler#compile(java.lang.String)
 */
  override def compile(text: String): (String, String) =
    try {
      val textXml = XML.loadString(text)
      val compiledAbstract = formatXmlString(compilePreviewAbstract(textXml).toString)
      val compiledText = formatXmlString(compilePost(textXml))
      (compiledAbstract, compiledText)
    } catch {
      case e: TextCompileException => throw e
      case e: Exception => throw new TextCompileException(e.getMessage())
    }

  private def formatXmlString(string: String): String =
    string.
      replaceAll("\n", "").
      replaceAll("\\s+", " ").
      trim

  private def compilePost(textXml: Elem): String = {
    val textAbstractCompiled1 = compileTextAbstract(textXml \ "abstract")
    val textCompiled1 = compileText(textXml \ "text")
    val footnotesCompiled1 =
      if (!footnotes.isEmpty)
        parseFootnotes()
      else
        NodeSeq.Empty
    val refsCompiled =
      if (!refs.isEmpty)
        parseReferences()
      else
        NodeSeq.Empty
    val contentsCompiled =
      if (!headers.isEmpty)
        parseContents()
      else
        throw new TextCompileException("Content is empty!")
    val textAbstractCompiled = compileIntRefs(textAbstractCompiled1)
    val textCompiled = compileIntRefs(textCompiled1)
    val footnotesCompiled = compileIntRefs(footnotesCompiled1)
    Xhtml.toXhtml(textAbstractCompiled ++ contentsCompiled ++ textCompiled ++ footnotesCompiled ++ refsCompiled)
  }

  private def compileText(nodes: NodeSeq): NodeSeq =
    nodes flatMap {
      case <text>{ contents @ _* }</text> => <div class="text">{ compile(contents) }</div>
      case _ => throw new TextCompileException("Could not find text tag in " + nodes + "!")
    }

  private def compileTextAbstract(nodes: NodeSeq): NodeSeq =
    nodes flatMap {
      case <abstract>{ contents @ _* }</abstract> => <div class="abstract">{ compile(contents) }</div>
      case _ => throw new TextCompileException("Could not find abstract tag in " + nodes + "!")
    }

  private def compile(nodes: NodeSeq): NodeSeq =
    nodes flatMap {
      case <p>{ contents @ _* }</p> => <p>{ compile(contents) }</p>
      case <b>{ contents @ _* }</b> => <b>{ compile(contents) }</b>
      case <i>{ contents @ _* }</i> => <i>{ compile(contents) }</i>
      case <code>{ contents @ _* }</code> => <code>{ compile(contents) }</code>
      case <ul>{ contents @ _* }</ul> => <ul>{ compile(contents) }</ul>
      case <li>{ contents @ _* }</li> => <li>{ compile(contents) }</li>
      case h @ <h1>{ titleNode }</h1> => compileHeader(h, titleNode, 1)
      case h @ <h2>{ titleNode }</h2> => compileHeader(h, titleNode, 2)
      case ref @ <ref>{ contents @ _* }</ref> => compileRef(ref, contents)
      case blockquote @ <blockquote>{ contents @ _* }</blockquote> => compileBlockquote(blockquote, contents)<commit_msg>changes <code> to <c> for sbtl<commit_after>package backend.engine

import scala.xml._
import play.api.Play

/**
 * A class implementing the compile method of the TextCompiler. The class is NOT thread safe!
 *
 * @author <NAME>
 *
 */
// TODO: Make it thread safe, use more functional style, make it less accepting of misused sbtl, testing...
class TextCompilerSbtl() extends TextCompiler {
  private val blogUrl = Play.current.configuration.getString("blogUrl").getOrElse("http://bleibinha.us/blog")
  private val baseBlogUrl = blogUrl.substring(0, blogUrl.indexOf("/blog"))
  private val externEvidence = "://"
  private val fileHostUrl = blogUrl + "/file/"
  private var footnotes: List[Footnote] = Nil
  private var footnotesNumber: Int = 1
  private var headers: List[List[Header]] = Nil
  private var refs: List[Ref] = Nil
  private var refNumber: Int = 1
  private var images: Int = 1
  private var codeSnippets: Int = 1
  private var internReferences: List[InternReference] = Nil

  /* (non-Javadoc)
 * @see backend.engine.TextCompiler#compile(java.lang.String)
 */
  override def compile(text: String): (String, String) =
    try {
      val textXml = XML.loadString(text)
      val compiledAbstract = formatXmlString(compilePreviewAbstract(textXml).toString)
      val compiledText = formatXmlString(compilePost(textXml))
      (compiledAbstract, compiledText)
    } catch {
      case e: TextCompileException => throw e
      case e: Exception => throw new TextCompileException(e.getMessage())
    }

  private def formatXmlString(string: String): String =
    string.
      replaceAll("\n", "").
      replaceAll("\\s+", " ").
      trim

  private def compilePost(textXml: Elem): String = {
    val textAbstractCompiled1 = compileTextAbstract(textXml \ "abstract")
    val textCompiled1 = compileText(textXml \ "text")
    val footnotesCompiled1 =
      if (!footnotes.isEmpty)
        parseFootnotes()
      else
        NodeSeq.Empty
    val refsCompiled =
      if (!refs.isEmpty)
        parseReferences()
      else
        NodeSeq.Empty
    val contentsCompiled =
      if (!headers.isEmpty)
        parseContents()
      else
        throw new TextCompileException("Content is empty!")
    val textAbstractCompiled = compileIntRefs(textAbstractCompiled1)
    val textCompiled = compileIntRefs(textCompiled1)
    val footnotesCompiled = compileIntRefs(footnotesCompiled1)
    Xhtml.toXhtml(textAbstractCompiled ++ contentsCompiled ++ textCompiled ++ footnotesCompiled ++ refsCompiled)
  }

  private def compileText(nodes: NodeSeq): NodeSeq =
    nodes flatMap {
      case <text>{ contents @ _* }</text> => <div class="text">{ compile(contents) }</div>
      case _ => throw new TextCompileException("Could not find text tag in " + nodes + "!")
    }

  private def compileTextAbstract(nodes: NodeSeq): NodeSeq =
    nodes flatMap {
      case <abstract>{ contents @ _* }</abstract> => <div class="abstract">{ compile(contents) }</div>
      case _ => throw new TextCompileException("Could not find abstract tag in " + nodes + "!")
    }

  private def compile(nodes: NodeSeq): NodeSeq =
    nodes flatMap {
      case <p>{ contents @ _* }</p> => <p>{ compile(contents) }</p>
      case <b>{ contents @ _* }</b> => <b>{ compile(contents) }</b>
      case <i>{ contents @ _* }</i> => <i>{ compile(contents) }</i>
      case <c>{ contents @ _* }</c> => <code>{ compile(contents) }</code>
      case <ul>{ contents @ _* }</ul> => <ul>{ compile(contents) }</ul>
      case <li>{ contents @ _* }</li> => <li>{ compile(contents) }</li>
      case h @ <h1>{ titleNode }</h1> => compileHeader(h, titleNode, 1)
      case h @ <h2>{ titleNode }</h2> => compileHeader(h, titleNode, 2)
      case ref @ <ref>{ contents @ _* }</ref> => compileRef(ref, contents)
      case blockquote @ <blockquote>{ contents @ _* }</blockquote> => compileBlockquote(blockquote, contents)
{'fraction_non_alphanumeric': 0.13402194964493222, 'fraction_numerical': 0.004389928986442866, 'mean_word_length': 3.5087310826542493, 'pattern_counts': {'":': 2, '<': 87, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757223', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2294, 'n_words': 652}