<commit_before>path = require 'path'
fs = require 'fs'
{Directory} = require 'atom'
processGraphs = require './process-graphs'
encrypt = require './encrypt'
CACHE = require './cache'
{fileImport} = require './file-import'
{protocolsWhiteListRegExp} = require './protocols-whitelist'

# TODO: refactor this file
# it has common functions as pandoc-convert.coffee

processMath = (text)->
  lines = text.split('\n')
  inBlock = false

  lines = lines.map (line)->
    if line.match(/^\s*```/)
      inBlock = !inBlock
      line
    else if inBlock
      line
    else
      line = line.replace(/\\\$/g, '#slash_dollarsign#')

      # display
      line = line.replace /\$\$([\s\S]+?)\$\$/g, ($0, $1)->
        $1 = $1.replace(/\n/g, '').replace(/\#slash\_dollarsign\#/g, '\\\$')
        $1 = escape($1)
        "<p align=\"center\"><img src=\"https://latex.codecogs.com/gif.latex?#{$1.trim()}\"/></p>"

      # inline
      r = /\$([\s\S]+?)\$/g
      line = line.replace /\$([\s\S]+?)\$/g, ($0, $1)->
        $1 = $1.replace(/\n/g, '').replace(/\#slash\_dollarsign\#/g, '\\\$')
        $1 = escape($1)
        "<img src=\"https://latex.codecogs.com/gif.latex?#{$1.trim()}\"/>"

      line = line.replace(/\#slash\_dollarsign\#/g, '\\\$')
  lines.join('\n')

# convert relative path to project path
processPaths = (text, fileDirectoryPath, projectDirectoryPath, useAbsoluteImagePath)->
  match = null
  offset = 0
  output = ''

  resolvePath = (src)->
    if src.match(protocolsWhiteListRegExp)
      return src

    if useAbsoluteImagePath
      if src.startsWith('/')
        return src
      else # ./test.png or test.png
        return '/' + path.relative(projectDirectoryPath, path.resolve(fileDirectoryPath, src))
    else
      if src.startsWith('/')
        return path.relative(fileDirectoryPath, path.resolve(projectDirectoryPath, '.'+src))<commit_msg>better math export for markdown export.<commit_after>path = require 'path'
fs = require 'fs'
{Directory} = require 'atom'
processGraphs = require './process-graphs'
encrypt = require './encrypt'
CACHE = require './cache'
{fileImport} = require './file-import'
{protocolsWhiteListRegExp} = require './protocols-whitelist'

# TODO: refactor this file
# it has common functions as pandoc-convert.coffee

processMath = (text)->
  line = text.replace(/\\\$/g, '#slash_dollarsign#')

  inline = JSON.parse(atom.config.get('markdown-preview-enhanced.indicatorForMathRenderingInline'))
  block = JSON.parse(atom.config.get('markdown-preview-enhanced.indicatorForMathRenderingBlock'))

  inlineBegin = '(?:' + inline.map (x)-> x[0]
                      .join('|')
                      .replace(/\\/g, '\\\\')
                      .replace(/([\(\)\[\]\$])/g, '\\$1') + ')'
  inlineEnd = '(?:' + inline.map (x)-> x[1]
                      .join('|')
                      .replace(/\\/g, '\\\\')
                      .replace(/([\(\)\[\]\$])/g, '\\$1') + ')'
  blockBegin = '(?:' + block.map (x)-> x[0]
                      .join('|')
                      .replace(/\\/g, '\\\\')
                      .replace(/([\(\)\[\]\$])/g, '\\$1') + ')'
  blockEnd = '(?:' + block.map (x)-> x[1]
                      .join('|')
                      .replace(/\\/g, '\\\\')
                      .replace(/([\(\)\[\]\$])/g, '\\$1') + ')'

  # display
  line = line.replace new RegExp("(```(?:[\\s\\S]+?)```\\s*(?:\\n|$))|(?:#{blockBegin}([\\s\\S]+?)#{blockEnd})", 'g'), ($0, $1, $2)->
    return $1 if $1
    math = $2
    math = math.replace(/\n/g, '').replace(/\#slash\_dollarsign\#/g, '\\\$')
    math = escape(math)
    "<p align=\"center\"><img src=\"https://latex.codecogs.com/gif.latex?#{math.trim()}\"/></p>"

  # inline
  console.log "(```(?:[\\s\\S]+?)```\\s*(?:\\n|$))|(?:#{inlineBegin}([\\s\\S]+?)#{inlineEnd})"
  line = line.replace new RegExp("(```(?:[\\s\\S]+?)```\\s*(?:\\n|$))|(?:#{inlineBegin}([\\s\\S]+?)#{inlineEnd})", 'g'), ($0, $1, $2)->
    return $1 if $1
    math = $2
    math = math.replace(/\n/g, '').replace(/\#slash\_dollarsign\#/g, '\\\$')
    math = escape(math)
    "<img src=\"https://latex.codecogs.com/gif.latex?#{math.trim()}\"/>"

  line = line.replace(/\#slash\_dollarsign\#/g, '\\\$')
  return line

# convert relative path to project path
processPaths = (text, fileDirectoryPath, projectDirectoryPath, useAbsoluteImagePath)->
  match = null
  offset = 0
  output = ''

  resolvePath = (src)->
    if src.match(protocolsWhiteListRegExp)
      return src

    if useAbsoluteImagePath
      if src.startsWith('/')
        return src
      else # ./test.png or test.png
        return '/' + path.relative(projectDirectoryPath, path.resolve(fileDirectoryPath, src))
    else
      if src.startsWith('/')
        return path.relative(fileDirectoryPath, path.resolve(projectDirectoryPath, '.'+src))
{'fraction_non_alphanumeric': 0.23807531380753139, 'fraction_numerical': 0.007531380753138075, 'mean_word_length': 2.439568345323741, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, 'https://': 4, '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': '4376299', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1617, 'n_words': 304}