Skip to content

Instantly share code, notes, and snippets.

@166MMX
Last active August 29, 2015 14:05
Show Gist options
  • Save 166MMX/e936d5e40f305d8b13b1 to your computer and use it in GitHub Desktop.
Save 166MMX/e936d5e40f305d8b13b1 to your computer and use it in GitHub Desktop.
package com.initvoid.jconfig
import groovy.transform.CompileStatic
import java.util.regex.Pattern
@CompileStatic
class PreProcessor
{
static Pattern HELP_PATTERN = ~/^\s*(---)?\s*help\s*(---)?/
static int INIT_LENGTH = -1
static void process (InputStream inputStream, OutputStream outputStream)
{
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)
InputStreamReader inputStreamReader = new InputStreamReader(inputStream)
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)
String line
boolean active = false
int baseIndent = INIT_LENGTH
while ((line = bufferedReader.readLine()) != null)
{
if (line.matches(HELP_PATTERN))
{
active = true
baseIndent = INIT_LENGTH
}
else if (active)
{
int indentLength = indentLength(line)
if (baseIndent == INIT_LENGTH)
{
baseIndent = indentLength
}
else if (indentLength < baseIndent && line.trim().length() > 0)
{
outputStreamWriter.write('\u001F')
active = false
}
}
outputStreamWriter.write(line)
outputStreamWriter.write('\n')
}
if (active)
{
outputStreamWriter.write('\u001F')
}
outputStreamWriter.flush()
}
static int indentLength (String line)
{
int lineLength = line.length()
int indentLength = -1
for (int i = 0; i < lineLength; i++)
if (line[i] == '\t')
indentLength = (indentLength & ~7) + 8
else if (line[i] == ' ')
indentLength++
else
break
return indentLength
}
static void main (String[] args)
{
def url = 'https://raw.githubusercontent.com/torvalds/linux/master/arch/x86/Kconfig'.toURL()
def bufferedInputStream = url.newInputStream()
def byteArrayOutputStream = new ByteArrayOutputStream()
process(bufferedInputStream, byteArrayOutputStream)
def result = byteArrayOutputStream.toString()
println result
'nop'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment