nocomment

package module
v0.0.0-...-50d3229 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2017 License: Apache-2.0 Imports: 4 Imported by: 1

README

nocomment

Build Status GoDoc License

Nocomment removes comments. Comments can either be line comments or block comments.

Comments

Comments will be elided from the text if the beginning comment delimiter is found and is not within quoted text: quoted text starts with a " and ends with a ".

Support for single-quotes, ', and/or raw quotes ` may be added.

Line comment

For line comments, by default, nocomment interprets # and // as the beginning of a line comment. Line comments are terminated when an EOL is encountered: \r, \n, or \r\n.

There are toggles for whether or not an octothorpe (hash), #, or a double slash, // should be accepted as the beginning of a line comment. By default, both types of line comments are enabled. Disabling both types of line comments will result in line comments being preserved.

Configuration of line comments can only be done when using the Stripper struct.

Block comment

Nocomment uses C style block comments, /* */. Block comments may span new lines.

Usage

Input is expected to be []byte and the cleaned input is returned as []byte.

import github.com/mohae/nocomment

To elide comments from text using nocomment's defaults:

cleaned := Clean(input)

If you want to configure what is accepted as line-comments, the Stripper struct must be used. This struct has exported methods which accept bool and allow for the toggling of line comment types: Stripper.SetIgnoreHash() and Stripper.SetIgnoreSlash().

Example

In this example, only // are allowed as line comments:

s := NewStripper()
s.SetIgnoreHash(true) // do not accept # as the start of a line comment.

Once set, the input is cleaned with the Clean() method, which accepts and returns []byte

cleaned := s.Clean(input)

Docs:

https://godoc.org/github.com/mohae/nocomment

Notes:

This is based on Rob Pike's lexer design. There is code in the package that has been copied from the source code within http://golang.org/src/text/template/parse/. The files containing any copied code also have the original copyright notices.

For license and copyright information on this package, please read the LICENSE file.

Documentation

Overview

Package nocomment removes line and block comments from the provided bytes.

Line comments start with either // or # and end when an EOL is encountered. What to accept as line comments is configurable.

Block comments start with /* and end with */ and can span lines.

Anything within quotes, "", is ignored.

Index

Constants

View Source
const (

	// C++ style comments
	CPPComment commentType
	// shell style comments
	ShellComment
	// C style comments
	CComment
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Pos

type Pos int

Pos is a byte position in the original input text.

type Stripper

type Stripper struct {
	// KeepCComments: do not elide C style comments (/* */).
	KeepCComments bool
	// KeepCPPComments: do not elide C++ style comments (//).
	KeepCPPComments bool
	// KeepShellComments: do not elide C style comments (#).
	KeepShellComments bool
}

Stripper handles the elision of comments from text. The style of comments to elide is configurable: all supported styles are elided by default.

func (*Stripper) Clean

func (s *Stripper) Clean(input []byte) (b []byte, err error)

Clean removes comments from the input.

Directories

Path Synopsis
cmd
nocomment command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL