Skip to content

Infotition/petron

Repository files navigation


infotition logo

Petron

Petron is a carbon based code formatting API.



Lizens Discord server


About

Petron is a carbon based code formatting API. It uses the npm module pupetteer to visit https://carbon.now.sh with custom configuration in the request header in a faceless browser. To save the picture, the api takes a screenshot of the code field to save it on a public url for 24 hours.

This project uses carbon and also was heavily inspired by carbonara.

Getting Started

For now the api is only deployed for Infotition so you have to deploy/host it for yourself.

Development

Requirements: Node.js for running javascript code without a browser and NPM (Node package manager) to install npm modules.

If you want to start it locally from source code, you can simply clone this repository from terminal and install all the node modules.

# Clone this git repository
git clone https://github.com/Infotition/petron.git

# Move to the project root directory
cd petron

# Install the dependencies with npm package manager
npm install

Now everything should be installed. For petron to work, you still have to create and configure some files.

If you want your API to be public you don't have to change anything. But if you want to be protected, then navigate to the config folder in your project directory and choose the environment you want to set the token (production or development). Now change the value of the TOKEN field to anything you want. Keep in mind, that this is your token secret, so something similar to a password and should be a random string.

Also Configure the server environments from production.env and development.env as you like. Now everything should be ready to start.

Open up a terminal and move to your project directory and run following command to start the server:

npm run start:dev 
# or
npm start

The command npm start uses the production.env file and is used for deployment while npm run start:dev the development.envfile for the local dev server.

Now the api is up and running and if everything worked, you can send now requests to it.

Docker

Requirements: Docker Desktop for Mac and Windows or Docker Engine for Linux.

If you just want to deploy or run the api server, then you can pull the provided docker container and run it:

# Pull the docker container from docker hub
docker pull infotition/petron
# Run the Container and open the Port 3000 to access it
docker run -p 3000:3000 -it --cap-add=SYS_ADMIN infotition/petron

You can also run docker-compose within the project directory after running the npm run webpack:build command.

docker-compose up --build

Routes

GET /api/petron/petronize

The Body of the request must be in JSON formatwith the following parameters (* means required):

parameter default type description
code* string Code snippet
token '' string Auth token for the api
format 'url string Response format
theme 'material' string Theme of the code
options '' object Configuration of carbon

The token parameter is only required, if the api is configured with an token. Also the token in the request must be equal to the one in node envrionment.

The format parameter defines the response of the api with following options:

  • 'url' - Sends the url of the image
  • 'img' - Sends the image as binary

The theme is a preset of options for quick requests. You can still define more options but the theme overwrites the equal paramaters in options. Supported Theme configurations (overwrites the option parameter):

  • panda, zenburn, solarized_light, material

You can choose as respone format between

  • url, image

The options Object must be constructed like so:

Field default type
paddingVertical '56px' string
paddingHorizontal '56px' string
backgroundColor 'rgba(41,45,62,1)' string
dropShadow true boolean
dropShadowOffsetY '3px' string
dropShadowBlurRadius '13px' string
theme 'material' string
windowTheme 'sharp' string
language 'auto' string
fontFamily 'Hack' string
fontSize '14px' string
lineHeight '133%' string
windowControls true boolean
widthAdjustment true boolean
lineNumbers false boolean
firstLineNumber 1 number
exportSize '4x' string
watermark false boolean

For easy way to configure the request visit https://carbon.now.sh, set the appearance you wish, click on settings (Gear Icon) → misc → export config to download a json your current settings. Now you can add the values to the equal parameters of the config object and send the request.


RESPONSE: URL to the image of the code snippet or the image itself.

Unsupported Parameters

Petron visits the carbon website with URL Headers which does not support every configuration possible on carbon:

  • backgroundImage, backgroundImageSelection, backgroundMode, squaredImage, hiddenCharacters, name, loading, icon, isVisible, width

Contributing

Before creating an issue, please ensure that it hasn't already been reported/suggested and double-check the current features.

See the contribution guide if you'd like to submit a Pull request.

Help

If you are experiencing problems while installing/coding petron, need hints or everything else you need another person to help you, please dont't hesitate to join our official Infotition Discord Server.

License

Petron is available under the MIT License, see the LICENSE file for more information.