GithubHelp home page GithubHelp logo

duguankui / slugify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fcingolani/slugify

0.0 0.0 0.0 163 KB

Simple Slug / Clean URL generator helper for Microsoft .NET framework.

Home Page: http://fcingolani.github.io/Slugify/

License: MIT License

C# 100.00%

slugify's Introduction

Slugify

Simple Slug / Clean URL generator helper for Microsoft .NET framework.

With default settings, you will get an hyphenized, lowercase, alphanumeric version of any string you please, with any diacritics removed and collapsed whitespace.

In example, having:

a ambição cerra o coração

You'll get:

a-ambicao-cerra-o-coracao

Installation

You can get the Slugify NuGet package by running the following command in the Package Manager Console:

PM> Install-Package Slugify

Basic Usage

It's really simple! Just instantiate SlugHelper and call its GenerateSlug with the string you want to convert; it'll return the URL Safe version:

using Slugify;

public class MyApp
{
   public static void Main()
   {
      SlugHelper helper = new SlugHelper();

      String title = "OLA ke ase!";

      String slug = helper.GenerateSlug(title); // "ola-ke-ase"

      Console.WriteLine(slug);
   }
}

Configuration

You can provide a SlugHelper.Config instance to SlugHelper's constructor to customize the helper's behavior, like so:

// Creating a configuration object
SlugHelper.Config config = new SlugHelper.Config();

// Replace spaces with a hyphens
config.CharacterReplacements.Add(" ", "-");

// We want a lowercase Slug
config.ForceLowerCase = true;

// Colapse consecutive whitespace chars into one
config.CollapseWhiteSpace = true;

// Remove everything that's not a letter, number, hyphen, dot, or underscore
config.DeniedCharactersRegex = @"[^a-zA-Z0-9\-\._]";

// Create a helper instance with our new configuration
SlugHelper helper = new SlugHelper(config);

In fact, last values are so common they're the default ones! So last code could be rewritten as:

SlugHelper.Config config = new SlugHelper.Config();
SlugHelper helper = new SlugHelper(config);

One more thing: SlugHelper.Config is used when you call the parameterless SlugHelper constructor. Then ...

SlugHelper helper = new SlugHelper();

... is the same as running the code we had in first place.

Options

CharacterReplacements

Type: Dictionary<String, String>. Default: [" ": "-"].

Will replace the specified keys with their associated value.

By default, will replace spaces with hyphens.

ForceLowerCase

Type: Boolean. Default: true.

Setting it to true will convert output string to be LowerCase. If false, original casing will be preserved.

CollapseWhiteSpace

Type: Boolean. Default: true.

Setting it to true will replace consecutive whitespace characters by just one space (" ").

DeniedCharactersRegex

Type: String. Default: [^a-zA-Z0-9\-\._].

Any character matching this Regular Expression will be deleted from the resulting string.

License

The MIT License (MIT)

Copyright (c) 2013 Federico Cingolani

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

slugify's People

Contributors

fcingolani avatar eerrecart avatar

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.