Giter Site home page Giter Site logo

processr's Introduction

Processr

  • Overview

  • Installation

  • Usage

  • Bugs

Overview

Processr is a simple text processing and concatenation library. It takes a number of input strings (or files) and outputs a single string (or file) containing the result. Text can be passed through filters to modify the output.

Installation

The project is hosted on rubygems.org. Getting it is simple:

gem install processr

Usage

Configuration

Use the configuration block to setup Processr

Processr.configure do |config|
  config.root = File.expand_path(File.dirname(__FILE__))
  config.out  = File.join(config.root, 'output.txt')
end

If an output file is specified the result will be written to that file, otherwise the result will be returned directly from the #process! method.

Basic

processor = Processr.new
processor << "Some\n"
processor << "Text"
processor.process! # => "Some\nText"

This will result in a concatenated string being returned.

Basic (from file)

processor = Processr.new
processor.files << 'input_one.txt'
processor.files << 'input_two.txt'
processor.process! # => contents of input_one.txt and input_two.txt

This will result in the contents of input_one.txt and input_two.txt returned.

Filters

Filters can be used to modify the output of a processing session. A filter is any object that responds to #call. Filters take a single argument (the input buffer) and must return the modified buffer for further processing. For example:

lambda do |buffer|
  # ...do something with buffer here...

  buffer
end

Or

class MyFilter

  def self.call(buffer)
    # ...do something with buffer here...

    buffer
  end

end

You can register a filter by calling #add_filter on an instance of Processr. A further example would be a really simple textile parser:

TextileFilter = lambda do |buffer|

  lookup = {
    /_(.*)_/         => '<em>\1</em>',
    /\*(.*)\*/       => '<strong>\1</strong>',
    /\"(.*)\":(\S*)/ => '<a href="\2">\1</a>'
  }

  lookup.each_pair do |regex, replacement|
    buffer.gsub!(regex, replacement)
  end

  buffer
end

processor = Processr.new
processor.add_filter(TextileFilter)
processor << 'A _simple_ example of a "textile":http://www.textism.com/tools/textile/ parser using a *filter*.'
processor.process! # => "A <em>simple</em> example of a <a href="http://www.textism.com/tools/textile/">textile</a> parser using a <strong>filter</strong>."

File Filters

You can also add filters to be run when a file is read to the buffer. This behaves similarly to a normal filter, and must return the file contents to the buffer:

InBetweenFilter = lambda do |filename, contents|
  puts "Filter for #{filename}"
  contents
end

processor = Processr.new
processor.add_file_filter(InBetweenFilter)
processor.files << File.join('..', 'spec', 'fixtures', 'one.txt')
processor.files << File.join('..', 'spec', 'fixtures', 'two.txt')
puts processor.process! => Filter for ../spec/fixtures/one.txt etc...

Run the examples for more information.

Bugs

If you have any problems with processr, please file an issue at github.com/joshnesbitt/processr/issues.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Josh Nesbitt <[email protected]>. See LICENSE for details.

processr's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

stacworks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.