Include a file into a markdown code-block

@param filename
@param language the programming language used for the code-block
@returns {string}
@access public
@memberOf helpers
485004
function include (filename, language) {
  return fs.readFile(filename, 'utf-8').then(function (contents) {
    return '```' +
      (typeof language === 'string' ? language : path.extname(filename).substr(1)) +
      '\n' +
      contents +
      '\n```\n'
  })
}
308221
public function tokenize($code, $filename = null)
    {
        if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
            $encoding = mb_internal_encoding();
            mb_internal_encoding('ASCII');
        }

        $this->code = str_replace(array("\r\n", "\r"), "\n", $code);
        $this->source = '';
        $this->filename = $filename;
        $this->cursor = 0;
        $this->lineno = 1;
        $this->end = strlen($this->code);
        $this->tokens = array();
        $this->state = self::STATE_DATA;
        $this->states = array();
        $this->brackets = array();
        $this->position = -1;

        preg_match_all(self::REGEX_CHAR, $this->code, $this->positions, PREG_OFFSET_CAPTURE);

        while ($this->cursor < $this->end) {
            switch ($this->state) {
                case self::STATE_DATA:
                    $this->lexData();
                    break;

                case self::STATE_OUTPUT:
                    $this->lexOutput();
                    break;

                case self::STATE_DIRECTIVE:
                    $this->lexDirective();
                    break;
            }
        }

        if ($this->state != self::STATE_DATA) {
            $this->addCode(' ?>');
            $this->popState();
        }

        if (!empty($this->brackets)) {
            list($expect, $lineno) = array_pop($this->brackets);
            throw new SyntaxErrorException(sprintf('Unclosed "%s" at line %d in file %s', $expect, $lineno, $this->filename));
        }

        if (isset($encoding)) {
            mb_internal_encoding($encoding);
        }

        return new TokenStream(token_get_all($this->source));
    }
691303
function generateEmbed(config, file) {
  const targetPath = path.join(config.destDir, path.relative(config.src, file));
  const document = parseDocument(file);
  const sampleSections = document.sections.filter(
    s => s.inBody && !s.isEmptyCodeSection()
  );
  sampleSections.forEach((section, index) => {
    highlight(section.codeSnippet()).then(code => {
      const tag = section.doc ? slug(section.doc.toLowerCase()) : index;
      const context = {
        sample: {
          file: path.basename(targetPath),
          preview: addFlag(targetPath, tag, 'preview'),
          embed: addFlag(targetPath, tag, 'embed'),
          template: addFlag(targetPath, tag, 'template'),
          body: section.code,
          code: code,
        },
        config: config,
        document: document,
      };
      generate(context.sample.preview, templates.preview, context, /* minify */ true);
      generate(context.sample.embed, templates.embed, context, /* minify */ true);
      generateTemplate(context);
    });
  });
}
56219
function getFileEntryDisplay(entry) {
        var name = entry.name,
            ext = LanguageManager.getCompoundFileExtension(name),
            i = name.lastIndexOf("." + ext);

        if (i > 0) {
            // Escape all HTML-sensitive characters in filename.
            name = _.escape(name.substring(0, i)) + "<span class='extension'>" + _.escape(name.substring(i)) + "</span>";
        } else {
            name = _.escape(name);
        }

        return name;
    }
873226
function Markdown(runner, options) {
  Base.call(this, runner, options);

  var level = 0;
  var buf = '';

  function title(str) {
    return Array(level).join('#') + ' ' + str;
  }

  function mapTOC(suite, obj) {
    var ret = obj;
    var key = SUITE_PREFIX + suite.title;

    obj = obj[key] = obj[key] || {suite: suite};
    suite.suites.forEach(function(suite) {
      mapTOC(suite, obj);
    });

    return ret;
  }

  function stringifyTOC(obj, level) {
    ++level;
    var buf = '';
    var link;
    for (var key in obj) {
      if (key === 'suite') {
        continue;
      }
      if (key !== SUITE_PREFIX) {
        link = ' - [' + key.substring(1) + ']';
        link += '(#' + utils.slug(obj[key].suite.fullTitle()) + ')\n';
        buf += Array(level).join('  ') + link;
      }
      buf += stringifyTOC(obj[key], level);
    }
    return buf;
  }

  function generateTOC(suite) {
    var obj = mapTOC(suite, {});
    return stringifyTOC(obj, 0);
  }

  generateTOC(runner.suite);

  runner.on(EVENT_SUITE_BEGIN, function(suite) {
    ++level;
    var slug = utils.slug(suite.fullTitle());
    buf += '<a name="' + slug + '"></a>' + '\n';
    buf += title(suite.title) + '\n';
  });

  runner.on(EVENT_SUITE_END, function() {
    --level;
  });

  runner.on(EVENT_TEST_PASS, function(test) {
    var code = utils.clean(test.body);
    buf += test.title + '.\n';
    buf += '\n```js\n';
    buf += code + '\n';
    buf += '```\n\n';
  });

  runner.once(EVENT_RUN_END, function() {
    process.stdout.write('# TOC\n');
    process.stdout.write(generateTOC(runner.suite));
    process.stdout.write(buf);
  });
}
608769
function(input, context, filename, callback) {
                var compress, error, mode, output, parse, ref1, ref2, variables;
                input = input.replace(/\uFF00/g, '\n');
                input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
                context = _options.context || context;
                variables = _options.variables || (_options.variables = {});
                error = [];
                ref1 = _options.command, compile = ref1.compile, evaluate = ref1.evaluate, parse = ref1.parse, render = ref1.render, stringify = ref1.stringify;
                ref2 = _options.flags, mode = ref2.mode, compress = ref2.compress;
                Expression.callback(function(expression, result, stack, errors) {
                    var key, ref3, value;
                    context['_'] = result;
                    ref3 = stack.local;
                    for (key in ref3) {
                        if (!hasProp.call(ref3, key)) continue;
                        value = ref3[key];
                        variables[key] = value;
                    }
                    if (errors != null) {
                        error = error.concat(errors);
                    }
                });
                try {
                    output = (function() {
                        switch (mode) {
                            case 'c':
                                return compile(input, null, compress);
                            case 'p':
                                return stringify(input, null, compress);
                            case 'r':
                                return render(input, null, compress);
                            case 's':
                                return parse(input);
                            default:
                                return evaluate(input, context, variables);
                        }
                    })();
                    callback(_prettyErrorMessage(error, filename, input, true), output);
                } catch (error1) {
                    error = error1;
                    callback(_prettyErrorMessage(error, filename, input, true));
                }
            }
389027
function markdown(text, inline) {
    var r;

    if (text !== undefined)
      r = md_conrefs.replaceConref(text);

    else
      r = "";

    r = namp(r).html;

    // inline markdown means to strip enclosing tag, which in this case is <p>
    if (inline === true) {
        r = r.slice(3, -5);
    }

    // FIXME
    // desugar [[foo#bar]] tokens into local links
    // N.B. in order to not apply conversion in <code> blocks,
    // we first store replace code blocks with nonces
    var codes = {};
    r = r.replace(/(<code>[\s\S]*?<\/code>)/g, function (all, def) {
      var nonce = Math.random().toString().substring(2);
      codes[nonce] = def;
      return '@-=@=-@' + nonce + '@-=@=-@';
    });

    // convert [[link]] to links
    r = r.replace(/\[\[([\s\S]+?)\]\]/g, function (all, def) {
      def = def.split(/\s+/);
      id = def.shift();
      // invalid references don't produce links
      /*if (!list[id]) {// it's in a different file--list only refers to current page
          obj = fullList[id];

          if (obj === undefined) { // it might be global
              obj = {};
              obj.id = id;
          }
      }*/
      //else {
          obj = list[id];
      //}

      var obj = _.extend({name: def.join(' ') || id, globalId: id}, list[id]);
      obj.name = def.join(' ') || id;

      // lame, but I'm tired of writing stuff like [[child_process.fork `child_process.fork()`]]
      // If I didn't do this, it'd come out as an unstyled link
      if (def.length == 0) {
        if (obj.type === undefined && additionalObjsJSON[obj.name] === undefined && obj.name !== "Mixed") {
          console.error("Error".red + ": you tried to create a short link to an object that doesn't exist!");
          console.error(obj);
        } 

        if (additionalObjsJSON[obj.name] !== undefined)
          obj.name = "<code>" + obj.name + "</code>";
        else if (obj.type && obj.type !== "class" && obj.type.indexOf("property") < 0)
          obj.name = "<code>" + obj.name + "()</code>";
        else
          obj.name = "<code>" + obj.name + "</code>";
      }

      return link(obj, ['link-short'], true);
    });

    // restore code blocks, given previously stored nonces
    r = r.replace(/@-=@=-@(\d+)@-=@=-@/g, function (all, nonce) {
      return codes[nonce];
    });

    return r;
  }
1202391
function _buildContext(filename, languageId) {
        var ctx = {};
        if (filename) {
            ctx.path = filename;
        } else {
            ctx.path = currentFilename;
        }
        if (languageId) {
            ctx.language = languageId;
        } else {
            ctx.language = currentLanguageId;
        }
        ctx.scopeOrder = _includeProjectScope(ctx.path) ?
                        scopeOrderWithProject :
                        scopeOrderWithoutProject;
        return ctx;
    }
1141692
function (code, language, bare) {
    var result;
    if (typeof hljs !== 'undefined') {
      if (language) {
        try {
          result = hljs.highlight(language, code, true);
        }
        catch (e) {
          Ember.warn(
            '[marked] Failing to highlight some code with language `' + language +
            '`, trying auto-detecting language (error: ' + e + ').'
          );
          result = hljs.highlightAuto(code);
        }
      }
      else {
        result = hljs.highlightAuto(code);
      }
      result.value = '<pre><code>' + result.value + '</code></pre>';
      if (bare) {
        return result;
      }
      else {
        return result.value;
      }
    }
    else {
      result = '<pre><code>' + (code || '').replace(/<>&/g, entitiesReplacer) + '</code></pre>';
      if (bare) {
        return {value: result, language: null};
      }
      else {
        return result;
      }
    }
  }
909606
function generateMarkdown (doc, functions) {
  let text = ''

  // TODO: should escape HTML characters in text

  text += '<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->\n\n'

  text += '# Function ' + doc.name + '\n\n'

  text += doc.description + '\n\n\n'

  if (doc.syntax && doc.syntax.length) {
    text += '## Syntax\n\n' +
        '```js\n' +
        doc.syntax.join('\n') +
        '\n```\n\n'
  }

  if (doc.where && doc.where.length) {
    text += '### Where\n\n' + doc.where.join('\n') + '\n\n'
  }

  text += '### Parameters\n\n' +
      'Parameter | Type | Description\n' +
      '--------- | ---- | -----------\n' +
      doc.parameters.map(function (p) {
        return '`' + p.name + '` | ' +
            (p.types ? p.types.join(' &#124; ') : '') + ' | ' +
            p.description
      }).join('\n') +
      '\n\n'

  if (doc.returns) {
    text += '### Returns\n\n' +
        'Type | Description\n' +
        '---- | -----------\n' +
        (doc.returns.types ? doc.returns.types.join(' &#124; ') : '') + ' | ' + doc.returns.description +
        '\n\n\n'
  }

  if (doc.examples && doc.examples.length) {
    text += '## Examples\n\n' +
        '```js\n' +
        doc.examples.join('\n') +
        '\n```\n\n\n'
  }

  if (doc.seeAlso && doc.seeAlso.length) {
    text += '## See also\n\n' +
        doc.seeAlso.map(function (name) {
          return '[' + name + '](' + name + '.md)'
        }).join(',\n') +
        '\n'
  }

  return text
}
1269417
function extractCodeBlocks(filepath) {
  return new Promise(function(fulfill, reject) {
    fs.readFile(filepath, 'utf8', function(err, text) {
      if (err) {
        console.log(err);
        reject(err);
      } else {
        var converter = new showdown.Converter();
        var html = converter.makeHtml(text);
        var $ = cheerio.load(html);
        var codeBlocks = $('pre code').map(function(index, elem) {
          var language = ($(elem).attr('class') || 'none language-none').split(' ')[0];
          return {
            filename: buildFilename(filepath, elem, $),
            content: $(elem).text(),
            language: language
          };
        });
        fulfill(codeBlocks.toArray());
      }
    });
  });
}
1112471
function readFileContent(fileName){
    try{
        return(fs.readFileSync(fileName , "utf-8"));
    }catch(e){
        console.log("---readFileContent-- Error reading file "+fileName);
        console.log(e);
        return null;
    }
}
850706
function parseLine(language) { return function(line) {
  return language.isEmpty(line)?    Blank.make()
  :      language.isComment(line)?  Text.make(language.parseComment(line))
  :      /* otherwise */            Code.make(line, language.name) }}
937641
private function langFile($fileName = '', $lang = '')
    {
        if (empty($lang)) {
            $lang = $this->getLangBrowserByCurrentUser();
            if (!$lang) {
                $lang = $this->config->languageBrowser();
            }
        }

        $filePath = $this->langFilePath($lang, $fileName);

        if (!file_exists($filePath)) {
            if ($lang === $this->config->defaultLanguage()) {
                throw new MissingLangFileException($filePath);
            }
            return $this->langFile($fileName, $this->config->defaultLanguage());
        }

        return require $filePath;
    }
1167395
function encodeGlobal(output) {
    var book = output.getBook();
    var bookFS = book.getContentFS();
    var logger = output.getLogger();
    var outputFolder = output.getRoot();
    var plugins = output.getPlugins();
    var blocks = Plugins.listBlocks(plugins);

    var result = {
        log: logger,
        config: encodeConfig(output, book.getConfig()),
        summary: encodeSummary(output, book.getSummary()),

        /**
            Check if the book is a multilingual book

            @return {Boolean}
        */
        isMultilingual: function() {
            return book.isMultilingual();
        },

        /**
            Check if the book is a language book for a multilingual book

            @return {Boolean}
        */
        isLanguageBook: function() {
            return book.isLanguageBook();
        },

        /**
            Read a file from the book

            @param {String} fileName
            @return {Promise<Buffer>}
        */
        readFile: function(fileName) {
            return bookFS.read(fileName);
        },

        /**
            Read a file from the book as a string

            @param {String} fileName
            @return {Promise<String>}
        */
        readFileAsString: function(fileName) {
            return bookFS.readAsString(fileName);
        },

        /**
            Resolve a file from the book root

            @param {String} fileName
            @return {String}
        */
        resolve: function(fileName) {
            return path.resolve(book.getContentRoot(), fileName);
        },

        /**
            Resolve a page by it path

            @param {String} filePath
            @return {String}
        */
        getPageByPath: function(filePath) {
            var page = output.getPage(filePath);
            if (!page) return undefined;

            return encodePage(output, page);
        },

        /**
            Render a block of text (markdown/asciidoc)

            @param {String} type
            @param {String} text
            @return {Promise<String>}
        */
        renderBlock: function(type, text) {
            var parser = parsers.get(type);

            return parser.parsePage(text)
                .get('content');
        },

        /**
            Render an inline text (markdown/asciidoc)

            @param {String} type
            @param {String} text
            @return {Promise<String>}
        */
        renderInline: function(type, text) {
            var parser = parsers.get(type);

            return parser.parseInline(text)
                .get('content');
        },

        template: {
            /**
                Apply a templating block and returns its result

                @param {String} name
                @param {Object} blockData
                @return {Promise|Object}
            */
            applyBlock: function(name, blockData) {
                var block = blocks.get(name) || defaultBlocks.get(name);
                return Promise(block.applyBlock(blockData, result));
            }
        },

        output: {
            /**
                Name of the generator being used
                {String}
            */
            name: output.getGenerator(),

            /**
                Return absolute path to the root folder of output
                @return {String}
            */
            root: function() {
                return outputFolder;
            },

            /**
                Resolve a file from the output root

                @param {String} fileName
                @return {String}
            */
            resolve: function(fileName) {
                return path.resolve(outputFolder, fileName);
            },

            /**
                Convert a filepath into an url
                @return {String}
            */
            toURL: function(filePath) {
                return fileToURL(output, filePath);
            },

            /**
                Check that a file exists.

                @param {String} fileName
                @return {Promise}
            */
            hasFile: function(fileName, content) {
                return Promise()
                .then(function() {
                    var filePath = PathUtils.resolveInRoot(outputFolder, fileName);

                    return fs.exists(filePath);
                });
            },

            /**
                Write a file to the output folder,
                It creates the required folder

                @param {String} fileName
                @param {Buffer} content
                @return {Promise}
            */
            writeFile: function(fileName, content) {
                return Promise()
                .then(function() {
                    var filePath = PathUtils.resolveInRoot(outputFolder, fileName);

                    return fs.ensureFile(filePath)
                    .then(function() {
                        return fs.writeFile(filePath, content);
                    });
                });
            },

            /**
                Copy a file to the output folder
                It creates the required folder.

                @param {String} inputFile
                @param {String} outputFile
                @param {Buffer} content
                @return {Promise}
            */
            copyFile: function(inputFile, outputFile, content) {
                return Promise()
                .then(function() {
                    var outputFilePath = PathUtils.resolveInRoot(outputFolder, outputFile);

                    return fs.ensureFile(outputFilePath)
                    .then(function() {
                        return fs.copy(inputFile, outputFilePath);
                    });
                });
            }
        },

        gitbook: {
            version: gitbook.version
        }
    };

    // Deprecated properties

    deprecate.renamedMethod(output, 'this.isSubBook', result, 'isSubBook', 'isLanguageBook');
    deprecate.renamedMethod(output, 'this.contentLink', result, 'contentLink', 'output.toURL');

    deprecate.field(output, 'this.generator', result, 'generator',
        output.getGenerator(), '"this.generator" property is deprecated, use "this.output.name" instead');

    deprecate.field(output, 'this.navigation', result, 'navigation', function() {
        return encodeNavigation(output);
    }, '"navigation" property is deprecated');

    deprecate.field(output, 'this.book', result, 'book',
        result, '"book" property is deprecated, use "this" directly instead');

    deprecate.field(output, 'this.options', result, 'options',
        result.config.values, '"options" property is deprecated, use config.get(key) instead');

    return result;
}
513190
function (filePatterns, destination, options) {
  try {
    var fse = require('fs-extra')
  } catch (e) {
    console.error('This method can only be used in server applications.\n' +
      'Consider using the alternative method "i18nCompile.fromString()" instead.')
  }

  options = options || {};
  var matchedFiles = _.map(filePatterns, function (pattern) {
    return glob.sync(pattern);
  });

  var files = _.uniq(_.flatten(matchedFiles));

  var srcList = files.filter(function (filepath) {
    // Warn on and remove invalid source files (if nonull was set).
    try {
      fse.accessSync(filepath);
      return true;
    } catch (e) {
      console.warn('Source file "' + filepath + '" not found.');
      return false;
    }
  }).map(function (filepath) {
    // Read YAML file.
    var content = yaml.safeLoad(fse.readFileSync(filepath, 'utf8'), {
      filename: filepath,
      schema: yaml.JSON_SCHEMA
    });

    return new SrcItem(filepath, content);
  });

  var compiled = compileTranslations(srcList);

  // Handle options.
  if (options.merge) {
    // Write the destination file(with all languages merged in the same file).
    fse.outputFileSync(destination, JSON.stringify(compiled));

    // Print a success message.
    console.log('File "' + destination + '" created.');

    return;
  }

  // Write the destination files.
  for (var lang in compiled) {
    var fileDest = langFileDest(destination, lang, options.langPlace);
    fse.outputFileSync(fileDest, JSON.stringify(compiled[lang]));

    // Print a success message.
    console.log('File "' + fileDest + '" created.');
  }
}
338532
function writeAndRunCodeBlocks(codeBlocks) {
  var dir = '.runnable-markdown-' + rand.generateKey(7);
  return makeTempDir(dir)
  .then(function() {
    return new Promise(function(fulfill, reject) {
      async.mapSeries(codeBlocks, function(codeBlock, callback) {
        appendCodeBlockToFile(codeBlock, dir)
          .then(runCodeBlock)
          .then(function(codeBlock) {
            callback(null, codeBlock);
          })
          .catch(function(err) {
            callback(err, null);
          });
      }, convertAsyncResultToPromise(fulfill, reject));
    });
  })
  .then(function(codeBlocks) {
    return removeOldDir(dir)
      .then(function() {
        return codeBlocks;
      });
  });
}