<commit_before>package com.yuvimasory.flashcards

import java.io.{File, PrintWriter}

import StringUtils.LF

private[flashcards] abstract class ProgramTranslator extends FlashcardTranslator {

  val Delimit = "\t"
  val BR = "<br>"
  val PreOpen = "<pre>"
  val PreClose = "</pre>"

  override def translate(flashDoc: Document, sides: Pages.Value, outFile: File): Unit = {
    require(sides == Pages.All)
    val out = new PrintWriter(outFile)
    try {
      (0 until flashDoc.cards.length) foreach { i =>
        val card = flashDoc.cards(i)
        out print generateFront(card.front)
        out print Delimit
        out print (generateBack(card.back))
        out print LF
      }
    }
    finally {
      out flush()
      out close()
    }
  }

  def generateFront(front: Front): String = {
    val builder = new StringBuilder

    for (i <- 0 until front.frontEls.length) {
      front.frontEls(i) match {
        case FrontElement(Stretch(spans)) => {
          spans foreach {builder append generateHtmlSpan(_)}
          if (i < front.frontEls.length - 1) builder append (BR + BR)
        }
      }
    }

    builder toString
  }

  def generateBack(back: Back): String = {
    val builder = new StringBuilder

    for (i <- 0 until back.backEls.length) {
      back.backEls(i) match {
        case Line(stretch) =>
          stretch.spans foreach {builder append generateHtmlSpan(_)}
        case CodeBlock(lines) => {
          builder append PreOpen
          for (j <- 0 until lines.length) {
            builder append lines(j).extractText
            if (j < lines.length - 1) builder append BR
          }
          builder append PreClose
        }
        case LatexBlock(lines) => builder append handleLatex(lines.foldLeft("")(_ + _.extractText), true)
      }
      back.backEls(i) match {
        case Line(_)  =>  if (i < back.backEls.length - 1) builder append BR
        case CodeBlock(_) | LatexBlock(_) => //do nothing, newline is automatic

      }
    }

    builder toString
  }

  def generateHtmlSpan(span: Span) = {<commit_msg>insert newlines in latex<commit_after>package com.yuvimasory.flashcards

import java.io.{File, PrintWriter}

import StringUtils.LF

private[flashcards] abstract class ProgramTranslator extends FlashcardTranslator {

  val Delimit = "\t"
  val BR = "<br>"
  val PreOpen = "<pre>"
  val PreClose = "</pre>"

  override def translate(flashDoc: Document, sides: Pages.Value, outFile: File): Unit = {
    require(sides == Pages.All)
    val out = new PrintWriter(outFile)
    try {
      (0 until flashDoc.cards.length) foreach { i =>
        val card = flashDoc.cards(i)
        out print generateFront(card.front)
        out print Delimit
        out print (generateBack(card.back))
        out print LF
      }
    }
    finally {
      out flush()
      out close()
    }
  }

  def generateFront(front: Front): String = {
    val builder = new StringBuilder

    for (i <- 0 until front.frontEls.length) {
      front.frontEls(i) match {
        case FrontElement(Stretch(spans)) => {
          spans foreach {builder append generateHtmlSpan(_)}
          if (i < front.frontEls.length - 1) builder append (BR + BR)
        }
      }
    }

    builder toString
  }

  def generateBack(back: Back): String = {
    val builder = new StringBuilder

    for (i <- 0 until back.backEls.length) {
      back.backEls(i) match {
        case Line(stretch) =>
          stretch.spans foreach {builder append generateHtmlSpan(_)}
        case CodeBlock(lines) => {
          builder append PreOpen
          for (i <- 0 until lines.length) {
            builder append lines(i).extractText
            if (i < lines.length - 1) builder append BR
          }
          builder append PreClose
        }
        case LatexBlock(lines) => {
          val text = {
            val builder = new StringBuilder
            for (i <- 0 until lines.length) {
              builder append lines(i).extractText
              if (i < lines.length - 1) builder append BR
            }
            builder toString
          }
          builder append handleLatex(text, true)
        }
      }
      back.backEls(i) match {
        case Line(_)  =>  if (i < back.backEls.length - 1) builder append BR
        case CodeBlock(_) | LatexBlock(_) => //do nothing, newline is automatic

      }
    }

    builder toString
  }

  def generateHtmlSpan(span: Span) = {
{'fraction_non_alphanumeric': 0.1030881017257039, 'fraction_numerical': 0.0036330608537693005, 'mean_word_length': 2.1397006414825372, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '511094', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1285, 'n_words': 373}