项目作者: DRealestKMA

项目描述 :
Code highlighter api
高级语言: Vue
项目地址: git://github.com/DRealestKMA/highlighter.git
创建时间: 2020-10-02T17:15:55Z
项目社区:https://github.com/DRealestKMA/highlighter

开源协议:

下载


Highlighter

A code syntax highlighter REST api, which can handle over 500 languages and other text formats.

Features

  • Code highlighter.
    Used in highlighting programming languages used in blogs, chat apps and any project requiring code samples.

    • Formats Available (currently)

    • Styles Available
      default emacs friendly colorful autumn murphy manni monokai perldoc pastie borland trac native fruity bw vim vs tango rrt xcode igor paraiso-light paraiso-dark lovelace algol algol_nu arduino rainbow_dash abap solarized-dark solarized-light sas stata stata-light stata-dark inkpot

Run on your Local Machine (Docker)

To verify that no code snippet written on the minimal functionality code editor provided in the app are saved by me the developer, i advice that you clone this repo, go through its source code and run the docker-compose to see it live on you own machine. Note, you have to go into backend > gunicorn.conf.py and uncomment the contents within the file.

  1. $ docker-compose up --build

Please make sure you have docker all setup on your system, else get docker.

Request Config

Here are the config options for highlighting code snippets.

NOTE: this should be made through a POST request at all times as GET request is not supported.

  1. {
  2. // Code snippet to be highlighted. Required.
  3. code: '',
  4. // Programming language (sourcecode) of the code snippet to be highlighted. Required.
  5. language: 'python', // default
  6. // Syntax highlighting style.
  7. // checkout the available styles at the features section of this readme.
  8. style: 'default', // default
  9. // In what format would you like to retrieve your code snippet,
  10. // checkout the available formats at the features section of this readme.
  11. getFormat: 'html', // default
  12. // linenos is used to request if the result should have line numbers
  13. // or not, this can be one of ['inline', 'table', 'none'], defaults to
  14. // 'none', no line numbers. Optional.
  15. lineNos: 'none', // default
  16. // FORMAT: HTML => what type of html styling would you prefer? inline css or
  17. // classes, this can be one of ['inline', 'class']
  18. css: 'inline', // default
  19. // FORMAT: HTML => class name giving to the div tag wrapping the whole code block
  20. // defaults to 'justhighlight'. NOTE: if "lineNos" is set to table this
  21. // will append a 'table' to the class name making it for example 'justhighlighttable'.
  22. divClass: 'justhighlight', // default
  23. // hlLines is used to specify a list of line numbers to be highlighted in your code snippet
  24. // this should be an array of numbers. Defaults to an empty array.
  25. hlLines: [], // default
  26. // FORMAT: HTML => classPrefix is used to prefix the css classes used
  27. // when "css" is set "class".
  28. classPrefix: '' // default is an empty string
  29. // The noBackground option is used to request that the "style" option selected should not
  30. // affect the code background color. this should either be true/false, where true is
  31. // "do not apply background color" and false is "apply background color".
  32. noBackground: false, // default
  33. }

Response Data

The succefull response of a valid request contains the following.

  1. {
  2. // The status of the reesponse.
  3. status: 'success',
  4. // The result object containing the highlighted code data
  5. result: {
  6. data: '', // the highlighted code snippet.
  7. sourcecode: '', // the highlighted code return format.
  8. formatting: '', // the programming language highlighted.
  9. styles: '', // only available when styling is set to class.
  10. }
  11. }

Examples

  • Using axios API
  1. axios.post('https://api.justhighlight.com/highlighter/', {
  2. code: "const greet = 'Hello World!'",
  3. language: 'javascript',
  4. getFormat: 'html',
  5. style: 'paraiso-dark',
  6. lineNo: 'none',
  7. css: 'inline',
  8. divClass: 'player',
  9. hlLines: [],
  10. noBackground: false,
  11. })
  12. .then((response) => {
  13. // make use of the returned response as you will.
  14. this.highlighted = response.data.result.data;
  15. })
  16. .catch((error) => {
  17. // catch any error what so ever and debug it.
  18. console.log(error.response);
  19. });
  • Using fetch API
    1. // get all data needed.
    2. const data = {
    3. code: "const greet = 'Hello World!'",
    4. language: 'javascript',
    5. getFormat: 'html',
    6. style: 'paraiso-dark',
    7. lineNo: 'none',
    8. css: 'inline',
    9. divClass: 'player',
    10. hlLines: [],
    11. noBackground: false,
    12. };
    13. // make a post request using fetch api.
    14. fetch('https://api.justhighlight.com/highlighter/', {
    15. method: 'POST',
    16. headers: {'Content-Type': 'application/json'},
    17. body: JSON.stringify(data),
    18. })
    19. .then((response) => response.json())
    20. .then((data) => {
    21. // make use of the returned response as you will.
    22. this.highlighted = data.result.data;
    23. })
    24. .catch((error) => {
    25. // catch any error what so ever and debug it.
    26. console.log(error);
    27. });

Credits

Highlighter depends fully on Pygments which is a syntax highlighter written in Python. Checkout Pygments github repo and also Pygments official website for more insight.