<commit_before>'use strict'

utils = require 'src/utils'
pathUtils = require 'path'
fs = require 'fs-extra'
{Heading, Paragraph} = require '../markdown'

WIKI_INDEX_FILE_NAME = 'API-Reference.md'
SRC_INDEX_FILE = 'index.litcoffee'
SEP = '/'

filesHeadings = {}
filesTree = {}

exports.modifyFile = (file, path, mdPath) ->
    summary = ''

    for type, i in file
        if type instanceof Heading
            filesHeadings[path] = type.getHeadingText()
        if type instanceof Paragraph and type.text[0] is '>'
            summary = />\s*(.+)/.exec(type.text)[1]
            break
        if i > 0
            break

    filesTree[path] = Object.seal
        heading: filesHeadings[path]
        path: path
        mdPath: mdPath
        summary: summary
        children: []
        parent: null
    return

getFileWikiPageName = (file) ->
    pathUtils.parse(file.mdPath).name

treeToMd = (file, prefix = '') ->
    md = "#{prefix}* [[#{file.heading}|#{getFileWikiPageName(file)}]]"
    if file.summary
        md += " — #{file.summary}"
    md += '\n'

    prefix += '  '
    for child in file.children
        md += treeToMd child, prefix

    md

exports.onFilesParse = (parsedFiles, repo) ->
    indexFiles = {}

    # files to dirs
    for path, file of filesTree
        pathCfg = pathUtils.parse path
        if pathCfg.base is SRC_INDEX_FILE
            indexFiles[path] = file
            filesTree[pathCfg.dir] = file
        else
            fileDir = pathUtils.join pathCfg.dir, '/', pathCfg.name
            filesTree[fileDir] = file

    # children
    fileToParents = {}
    for path, file of filesTree
        pathDirname = pathUtils.dirname path
        pathParts = pathDirname.split SEP
        while pathParts.length
            parentFile = filesTree[pathParts.join(SEP)]
            if parentFile and parentFile isnt file
                fileToParents[file.path] = parentFile
                break
            pathParts.pop()
    for path, parentFile of fileToParents
        childFile = filesTree[path]
        childFile.parent = parentFile
        parentFile.children.push childFile

    # wiki index file
    md = '> [Wiki](Home)'
    md += ' ▸ **API Reference**\n\n'

    mdFilesHeadings = []
    mdFilesByHeadings = {}
    for path, file of indexFiles
        mdFilesHeadings.push file.heading
        mdFilesByHeadings[file.heading] = treeToMd file

    mdFilesHeadings.sort()

    for heading in mdFilesHeadings<commit_msg>API Reference list now is sorted<commit_after>'use strict'

utils = require 'src/utils'
pathUtils = require 'path'
fs = require 'fs-extra'
{Heading, Paragraph} = require '../markdown'

WIKI_INDEX_FILE_NAME = 'API-Reference.md'
SRC_INDEX_FILE = 'index.litcoffee'
SEP = '/'

filesHeadings = {}
filesTree = {}

exports.modifyFile = (file, path, mdPath) ->
    summary = ''

    for type, i in file
        if type instanceof Heading
            filesHeadings[path] = type.getHeadingText()
        if type instanceof Paragraph and type.text[0] is '>'
            summary = />\s*(.+)/.exec(type.text)[1]
            break
        if i > 0
            break

    filesTree[path] = Object.seal
        heading: filesHeadings[path]
        path: path
        mdPath: mdPath
        summary: summary
        children: []
        parent: null
    return

getFileWikiPageName = (file) ->
    pathUtils.parse(file.mdPath).name

treeToMd = (file, prefix = '') ->
    md = "#{prefix}* [[#{file.heading}|#{getFileWikiPageName(file)}]]"
    if file.summary
        md += " — #{file.summary}"
    md += '\n'

    prefix += '  '
    for child in file.children
        md += treeToMd child, prefix

    md

exports.onFilesParse = (parsedFiles, repo) ->
    indexFiles = {}

    # files to dirs
    for path, file of filesTree
        pathCfg = pathUtils.parse path
        if pathCfg.base is SRC_INDEX_FILE
            indexFiles[path] = file
            filesTree[pathCfg.dir] = file
        else
            fileDir = pathUtils.join pathCfg.dir, '/', pathCfg.name
            filesTree[fileDir] = file

    # children
    fileToParents = {}
    for path, file of filesTree
        pathDirname = pathUtils.dirname path
        pathParts = pathDirname.split SEP
        while pathParts.length
            parentFile = filesTree[pathParts.join(SEP)]
            if parentFile and parentFile isnt file
                fileToParents[file.path] = parentFile
                break
            pathParts.pop()
    for path, parentFile of fileToParents
        childFile = filesTree[path]
        childFile.parent = parentFile
        parentFile.children.push childFile

    # sort children
    for path, file of filesTree
        file.children.sort (file1, file2) ->
            if file1.heading < file2.heading
                -1
            else if file1.heading > file2.heading
                1
            else
                0

    # wiki index file
    md = '> [Wiki](Home)'
    md += ' ▸ **API Reference**\n\n'

    mdFilesHeadings = []
    mdFilesByHeadings = {}
    for path, file of indexFiles
        mdFilesHeadings.push file.heading
        mdFilesByHeadings[file.heading] = treeToMd file

    mdFilesHeadings.sort()

    for heading in mdFilesHeadings
{'fraction_non_alphanumeric': 0.10423076923076922, 'fraction_numerical': 0.0028846153846153848, 'mean_word_length': 2.1069295101553167, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 21, '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': '3505717', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1556, 'n_words': 397}