<commit_before>module.exports =
  by_category: (content) ->
    grouped = @_group_by_category(content)
    categories = @_to_json(grouped)

  _group_by_category: (content) ->
    grouped = []

    for c in content
      key = c.category
      if not grouped[key]? then grouped[key] = []
      grouped[key].push(c)

    return grouped

  _to_json: (grouped) ->
    categories = []

    for key, content_array of grouped
      json = (c.to_json() for c in content_array)
      category = { name: key, content: json }
      categories.push(category)

    return categories<commit_msg>add template object to category api<commit_after>fs = require('fs')
path = require('path')
config = require('../config')
Content = require('../content')

module.exports =
  by_category: (content) ->
    grouped = @_group_by_category(content)
    categories = @_to_json(grouped)

  _group_by_category: (content) ->
    grouped = []

    for c in content
      key = c.category
      if not grouped[key]? then grouped[key] = []
      grouped[key].push(c)

    return grouped

  _to_json: (grouped) ->
    categories = []

    for key, content_array of grouped
      json = (c.to_json() for c in content_array)

      category = {}

      template_path = path.join(key, '_template.md')
      if fs.existsSync(path.join(config.project_dir, config.content_dir, template_path))
        template = new Content(template_path)
        template.set('id', null)
        category.template = template.to_json()

      category.name = key
      category.content = json

      categories.push(category)

    return categories
{'fraction_non_alphanumeric': 0.11879321181646763, 'fraction_numerical': 0.0, 'mean_word_length': 2.9211822660098523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5866743', 'n_tokens_mistral': 511, 'n_tokens_neox': 486, 'n_words': 118}