Skip to content

Latest commit

 

History

History
130 lines (91 loc) · 3.19 KB

03to04.md

File metadata and controls

130 lines (91 loc) · 3.19 KB

Migration Guide from 0.3.x to 0.4.x

Version 0.4.0 introduced breaking changes in the API of tipograph. This short document should show you how to migrate your code to the version 0.4.0.

Import

There are no Replace and Languages submodules anymore. All the functionality and definitions lay in the tipograph default export.

0.3.x

// node
var replace = require('tipograph').Replace;

// browser
var replace = window.Tipograph.Replace;

0.4.0

// node
var tipograph = require('tipograph');

// browser
var tipograph = window.tipograph;

Configuration

In the old versions, tipograph was configurable through configure method of imported Replace module. However, with this solution one cannot have multiple differently configured replace functions. In 0.4.0, imported tipograph variable is actually a function which accepts the configuration and returns new replace function whose behavior is based on the configuration.

0.3.x

var languages = require('tipograph').Languages;

replace.configure(languages.czech);

0.4.0

var replace = tipograph({ language: 'czech' });

Replacement

There were several methods on Replace module which applied various rules (e.g., spaces, quotes, etc.), and all method which applied all of them. In 0.4.0, what rules should be applied is specified on initialization of replace function.

0.3.x

replace.spaces(text);
replace.all(text);

0.4.0

var replace1 = tipograph({ presets: ['spaces'] });
replace1(text);

var replace2 = tipograph(); // all presets are applied by default
replace2(text);

Custom Rules

In the past, custom rules were added rule by rule. This changed with introducing presets, and you are allowed to define your custom presets, which is just a function accepting a language parameter and returning a set of rules. Rule is just a pair of search value (string or regular expression) and its replacement (string or function). Note that this functionality actually didn't work in version 0.4.0, and was fixed in 0.4.4.

0.3.x

replace.addCustomRule(/-([a-z])/g, function (match, letter) {
    return letter.toUpperCase();
});

0.4.4

var custom = function (language) {
    // set of rules
    return [
        // rule is a pair of search value and its replacement
        [/-([a-z])/g, function (match, letter) {
            return letter.toUpperCase();
        }]
    ];
};

var replace = tipograph({ presets: tipograph.presets([custom]) }); // extend default presets with yours

Input Formats

In the old versions, HTML format was supported so the rules were not applied for example in HTML tags or inside code and pre tags. Version 0.4.0 goes much far (or at least, it looks much far). There is the possibility to specify an input format so special patterns in the input are not affected. See supported formats in the documentation.

0.3.x

// no (public) way how to set the input format, or even turn off the HTML preprocessing

0.4.0

var replace = tipograph({ format: 'html' });

Command Line

As of 0.4.0, tipograph offers also a command line interface.

tipograph -i input.txt -o output.txt -p spaces hyphens