<commit_before>import Action, { actions as action_registry } from '../core/action'
import Chart from '../models/items/chart'
import * as app from './app'
import manager from './manager'

declare var marked, hljs, Handlebars, moment, d3, $, ts

/**
 * Hook up syntax highlighting to marked' markdown processor, so code
 * blocks will be highlighted.
 */
marked.setOptions({
  highlight: function(code) {
    return hljs.highlightAuto(code).value
  }
})

export function register_helpers() {
  /**
   * Render markdown content to HTML.
   */
  Handlebars.registerHelper('markdown', function(value) {
    if (!value)
      return ''
    return new Handlebars.SafeString(marked(value))
  })

  /**
   * Render an object to syntax-highlighted JSON.
   */
  Handlebars.registerHelper('json', function(value) {
    return new Handlebars.SafeString(hljs.highlightAuto(JSON.stringify(value, null, '  ')).value)
  })

  /**
   * Format a datetime value in the timezone configured for the current
   * tessera user.
   */
  Handlebars.registerHelper('moment', function(format, value) {
    if (!value)
      return ''
    if (format == 'fromNow') {
      return moment(value).tz(app.config.DISPLAY_TIMEZONE).fromNow()
    } else {
      return moment(value).tz(app.config.DISPLAY_TIMEZONE).format(format)
    }
    return ''
  })

  Handlebars.registerHelper('format', function(format, value) {
    if (typeof(value) === 'undefined')
      return ''
    if (!format)
      return value
    try {
      return d3.format(format)(value)
    } catch ( e ) {
      console.log('Error formatting ' + format + ' / ' + value + ': ' + e.message)
      return value
    }
  })

  /**
   * Fetch the list of dashboards with a given tag and return a Markdown
   * list of links to them.
   */
  Handlebars.registerHelper('dashboards-tagged', function(tag) {
    let markdown = ''
    $.ajax({
      url: app.uri('/api/dashboard/tagged/' + tag),
      type: 'GET',
      async: false,
      success: function(data) {
        for (let d of data) {
          markdown += '  * ['
          if (d.category && d.category !== '') {
            markdown += d.category + ': '
          }
          markdown += d.title + ']('
          markdown += d.view_href + ')\n'
        }
      },<commit_msg>Log formatting errors with the logger, not just console.log()<commit_after>import Action, { actions as action_registry } from '../core/action'
import Chart from '../models/items/chart'
import * as app from './app'
import manager from './manager'
import { logger } from '../core/log'

declare var marked, hljs, Handlebars, moment, d3, $, ts

const log = logger('helpers')

/**
 * Hook up syntax highlighting to marked' markdown processor, so code
 * blocks will be highlighted.
 */
marked.setOptions({
  highlight: function(code) {
    return hljs.highlightAuto(code).value
  }
})

export function register_helpers() {
  /**
   * Render markdown content to HTML.
   */
  Handlebars.registerHelper('markdown', function(value) {
    if (!value)
      return ''
    return new Handlebars.SafeString(marked(value))
  })

  /**
   * Render an object to syntax-highlighted JSON.
   */
  Handlebars.registerHelper('json', function(value) {
    return new Handlebars.SafeString(hljs.highlightAuto(JSON.stringify(value, null, '  ')).value)
  })

  /**
   * Format a datetime value in the timezone configured for the current
   * tessera user.
   */
  Handlebars.registerHelper('moment', function(format, value) {
    if (!value)
      return ''
    if (format == 'fromNow') {
      return moment(value).tz(app.config.DISPLAY_TIMEZONE).fromNow()
    } else {
      return moment(value).tz(app.config.DISPLAY_TIMEZONE).format(format)
    }
    return ''
  })

  Handlebars.registerHelper('format', function(format, value) {
    if (typeof(value) === 'undefined')
      return ''
    if (!format)
      return value
    try {
      return d3.format(format)(value)
    } catch ( e ) {
      log.error('Error formatting ' + format + ' / ' + value + ': ' + e.message)
      return value
    }
  })

  /**
   * Fetch the list of dashboards with a given tag and return a Markdown
   * list of links to them.
   */
  Handlebars.registerHelper('dashboards-tagged', function(tag) {
    let markdown = ''
    $.ajax({
      url: app.uri('/api/dashboard/tagged/' + tag),
      type: 'GET',
      async: false,
      success: function(data) {
        for (let d of data) {
          markdown += '  * ['
          if (d.category && d.category !== '') {
            markdown += d.category + ': '
          }
          markdown += d.title + ']('
          markdown += d.view_href + ')\n'
        }
      },
{'fraction_non_alphanumeric': 0.14319552110249784, 'fraction_numerical': 0.0008613264427217916, 'mean_word_length': 2.6317435496481627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5310446', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1389, 'n_words': 365}