Skip to content

Commit

Permalink
docs: updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinaldy Rafli committed Aug 23, 2021
1 parent 60864b1 commit d916aea
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
83 changes: 60 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flourite - Language detector

A fork from [ts95/lang-detector](https://github.com/ts95/lang-detector), rewritten in Typescript with more language support.
A fork of [ts95/lang-detector](https://github.com/ts95/lang-detector), rewritten in Typescript with more language support.

Detects a programming language from a given string.

Expand All @@ -13,10 +13,11 @@ Detects a programming language from a given string.

| Languages | | |
| --------- | ---------- | ------ |
| C | Javascript | Python |
| C++ | Java | Ruby |
| HTML | CSS | PHP |
| SQL | Julia | Rust |
| C | HTML | PHP |
| C++ | Java | Python |
| C# | Javascript | Ruby |
| CSS | Julia | Rust |
| Go | Lua | SQL |

## Install

Expand Down Expand Up @@ -45,27 +46,63 @@ You could supply options to make see numbers of points for a certain language:
import flourite from 'flourite';

const code = flourite('printf("Hello World")', { statistics: true });
// {
// detected: 'C',
// statistics: [
// [ 'C', 1 ],
// [ 'Unknown', 1 ],
// [ 'C++', 0 ],
// [ 'CSS', 0 ],
// [ 'Go', 0 ],
// [ 'HTML', 0 ],
// [ 'Java', 0 ],
// [ 'Javascript', 0 ],
// [ 'Julia', 0 ],
// [ 'PHP', 0 ],
// [ 'Python', 0 ],
// [ 'Ruby', 0 ],
// [ 'Rust', 0 ],
// [ 'SQL', 0 ]
// ]

// code.detected = 'C'
// code.statistics = {
// C: 5,
// 'C++': 0,
// 'C#': 0,
// CSS: 0,
// Go: 0,
// HTML: 0,
// Java: 0,
// Javascript: 0,
// Julia: 0,
// Lua: -20,
// PHP: 0,
// Python: 0,
// Ruby: 0,
// Rust: 0,
// SQL: 0,
// Unknown: 1
// }
```

Or if you want to integrate it with [Shiki](https://github.com/shikijs/shiki), you could pass:

```js
const code = flourite('Console.WriteLine("Hello world!");', { shiki: true }); // => csharp
```

If you want to handle `Unknown` value, you could pass:

```js
const code = flourite("SELECT 'Hello world!' text FROM dual;", { noUnknown: true });
```

### With Typescript

```typescript
import flourite from 'flourite';
import type { Options, StatisticOutput } from 'flourite';

const flouriteOptions: Options = {
heuristic: true,
statistics: true,
};

const code = flourite('print!({:?}, &v);', flouriteOptions) as StatisticOutput;
```

### Available Options

| Key | Type | Default | Description |
| ---------- | --------- | ------- | ------------------------------------------------------------------------------------------------ |
| heuristic | `boolean` | `true` | Checks for codes on the top of the given input. Only checks when the lines of code is above 500. |
| statistics | `boolean` | `false` | If `true`, will return the statistics of all the guessed language. |
| shiki | `boolean` | `false` | Straightforward compatibility with Shiki's language specification type |
| noUnknown | `boolean` | `false` | If `true`, will not output `Unknown` on detected and statistics result |

## Development

- Use the Node.js version as defined on the `.nvmrc` file.
Expand Down
20 changes: 0 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,6 @@ import { SQL } from './languages/sql';
import { nearTop, getPoints } from './points';
import { convert } from './shiki';

/**
* A checker is an object with the following form:
* { pattern: /something/, points: 1 }
* or if the pattern only matches code near the top of a given file:
* { pattern: /something/, points: 2, nearTop: true }
*
* Key: Language name.
* Value: Array of checkers.
*
* N.B. An array of checkers shouldn't contain more regexes than
* necessary as it would inhibit performance.
*
* Points scale:
* 2 = Bonus points: Almost unique to a given language.
* 1 = Regular point: Not unique to a given language.
* -1 = Penalty point: Does not match a given language.
* Rare:
* -50 = Bonus penalty points: Only used when two languages are mixed together,
* and one has a higher precedence over the other one.
*/
const languages: Record<string, LanguagePattern[]> = {
C,
'C++': CPP,
Expand Down

0 comments on commit d916aea

Please sign in to comment.