nginx config file formatter/beautifier with no additional dependencies.
Project description
nginx config file formatter/beautifier
nginx config file formatter/beautifier written in Python with no additional dependencies. It can be used as a library or a standalone script. It formats nginx configuration files in a consistent way, described below:
- All lines are indented uniformly, with 4 spaces per level. The number of spaces is customizable.
- Neighboring empty lines are collapsed to at most two.
- Curly brace placement follows the Java convention.
- Whitespace is collapsed, except in comments and within quotation marks.
- Newline characters are normalized to the operating system default (LF or CRLF), but this can be overridden.
Installation
Python 3.4 or later is needed to run this program. The easiest way is to download the package from PyPI:
pip install nginxfmt
Manual installation
The simplest form of installation is copying nginxfmt.py
to your scripts' directory.
It has no third-party dependencies.
You can also clone the repository and symlink the executable:
cd
git clone https://github.com/slomkowski/nginx-config-formatter.git
ln -s ~/nginx-config-formatter/nginxfmt.py ~/bin/nginxfmt.py
Usage as standalone script
It can format one or several files.
By default, the result is saved to the original file, but it can be redirected to stdout.
It can also function in piping mode, using the --pipe
or -
switch.
usage: nginxfmt.py [-h] [-v] [-] [-p | -b] [-i INDENT] [--line-endings {auto,unix,windows,crlf,lf}] [config_files ...]
Formats nginx configuration files in consistent way.
positional arguments:
config_filesconfiguration files to format
options:
-h, --helpshow this help message and exit
-v, --verbose show formatted file names
-, --pipe reads content from standard input, prints result to stdout
-p, --print-resultprints result to stdout, original file is not changed
-b, --backup-original
backup original config file as filename.conf~
formatting options:
-i, --indent INDENT specify number of spaces for indentation
--line-endings {auto,unix,windows,crlf,lf}
specify line ending style: 'unix' or 'lf' for \n, 'windows' or 'crlf' for \r\n. When not provided, system-default is used
Using as library
The main logic is within the Formatter
class, which can be used in third-party code.
import nginxfmt
# initializing with standard FormatterOptions
f = nginxfmt.Formatter()
# format from string
formatted_text = f.format_string(unformatted_text)
# format file and save result to the same file
f.format_file(unformatted_file_path)
# format file and save result to the same file, original unformatted content is backed up
f.format_file(unformatted_file_path, backup_path)
Customizing formatting options:
import nginxfmt
fo = nginxfmt.FormatterOptions()
fo.indentation = 2# 2 spaces instead of default 4
fo.line_endings = '\n'# force Unix line endings
# initialize with standard FormatterOptions
f = nginxfmt.Formatter(fo)
Reporting bugs
Please create an issue at https://github.com/slomkowski/nginx-config-formatter/issues. Be sure to include config snippets to reproduce the issue, preferably:
- Snippet to be formatted
- Actual result with the invalid formatting
- Desired result
Credits
Copyright 2021 Michał Słomkowski. License: Apache 2.0. Previously published under https://github.com/1connect/nginx-config-formatter.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nginxfmt-1.3.0.tar.gz
.
File metadata
- Download URL: nginxfmt-1.3.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Linux/6.6.72-1-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
384d09b0d812c20d1b0112b580173b57b4e4266e6dbb9a53f9be6d613f4bd637
|
|
MD5 |
f49c44af29463404b557c914f8c7cbe1
|
|
BLAKE2b-256 |
d238190657c9ee2aee9d164ba1e06e72204b37b8325ade4640e3bb8753465c46
|
File details
Details for the file nginxfmt-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: nginxfmt-1.3.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Linux/6.6.72-1-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
85dda4b440ce10771f78f070e7a21c882eeec3a881cf0aace1037bca0e7ab675
|
|
MD5 |
c347b491cd0e47374ab74c62a3661c3d
|
|
BLAKE2b-256 |
ea367ef000f37b64558bebe93a2fd5959f11e14f239a6946663687d7b13a13b4
|