Skip to content

Commit

Permalink
Merge pull request #52 from teknologi-umum/language/elixir
Browse files Browse the repository at this point in the history
feat: elixir language support
  • Loading branch information
Reinaldy Rafli authored Oct 12, 2021
2 parents 20ff9c6 + 0afefdc commit 77a052d
Show file tree
Hide file tree
Showing 9 changed files with 668 additions and 945 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Detects a programming language from a given string.

## Detectable languages

| Languages | | | |
| --------- | ---------- | ------ | ------ |
| C | Dockerfile | Julia | Python |
| C++ | Go | Kotlin | Ruby |
| C# | HTML | Lua | Rust |
| Clojure | Java | Pascal | SQL |
| CSS | Javascript | PHP | Yaml |
| Languages | | | | |
| --------- | ---------- | ---------- | ------ | ---- |
| C | Dockerfile | Javascript | Pascal | SQL |
| C++ | Elixir | Julia | PHP | YAML |
| C# | Go | Kotlin | Python | |
| Clojure | HTML | Lua | Ruby | |
| CSS | Java | Markdown | Rust | |

## Install

Expand All @@ -39,32 +39,34 @@ or via a CDN (unpkg or jsdelivr)
```js
import flourite from 'flourite';

const code = flourite('printf("Hello World");');
const code = flourite('cout << "Hello world" << endl;');

// {
// language: 'C',
// language: 'C++',
// statistics: {
// C: 5,
// C: 0,
// Clojure: 0,
// 'C++': 0,
// 'C#': 0,
// 'C++': 5,
// CSS: 0,
// 'C#': 0,
// Dockerfile: 0,
// Elixir: 0,
// Go: 0,
// HTML: 0,
// Java: 0,
// Javascript: 0,
// Julia: 0,
// Julia: 2,
// Kotlin: 0,
// Lua: -20,
// Lua: 2,
// Markdown: 0,
// Pascal: 0,
// PHP: 0,
// Python: 0,
// Ruby: 0,
// Rust: 0,
// SQL: 0,
// Unknown: 1,
// YAML: 0,
// Unknown: 1
// },
// linesOfCode: 1
// }
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CPP } from './languages/cpp';
import { CS } from './languages/cs';
import { CSS } from './languages/css';
import { Dockerfile } from './languages/dockerfile';
import { Elixir } from './languages/elixir';
import { Go } from './languages/go';
import { HTML } from './languages/html';
import { Java } from './languages/java';
Expand All @@ -30,6 +31,7 @@ const languages: Record<string, LanguagePattern[]> = {
'C#': CS,
CSS,
Dockerfile,
Elixir,
Go,
HTML,
Java,
Expand Down
21 changes: 21 additions & 0 deletions src/languages/elixir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { LanguagePattern } from '../types';

export const Elixir: LanguagePattern[] = [
// Modules
{ pattern: /^\s*defmodule\s+.+\s+do$/, type: 'meta.module' },
// Alias
{ pattern: /\s*alias\s+.+as:.+/, type: 'keyword.other' },
// IO.puts()
{ pattern: /IO\.puts.+/, type: 'keyword.print' },
// Anonymous func
{ pattern: /fn\s+[A-Za-z0-9_:<>()]+\s+->\s+.+(end)?$/, type: 'keyword.function' },
{ pattern: /^\s*(def|defp)\s+.+\s+do$/, type: 'keyword.function' },
{ pattern: /^\s*(if|unless|cond|case|try|defimpl|defprotocol)\s+.+\s+do$/, type: 'keyword.control' },
{ pattern: /^\s*defstruct\s+/, type: 'keyword' },
// Spec
{ pattern: /^\s*@spec\s+.+::.+/, type: 'macro' },
// Lists
{ pattern: /\{:.+,.+\}/, type: 'constant.array' },
// Maps
{ pattern: /%\{(.+(=>|:).+(,)?){1,}\}/, type: 'constant.dictionary' },
];
26 changes: 10 additions & 16 deletions src/languages/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ import type { LanguagePattern } from '../types';

export const Markdown: LanguagePattern[] = [
// headings
{ pattern: /(#){1,6} .+/, type: 'macro', nearTop: true },
{ pattern: /^(#){2,6}\s.+/, type: 'keyword' },
// headings alternate syntax
{ pattern: /^(?!!)(=|-){2,}(?<!>)$/, type: 'meta.module', nearTop: true },
// bold
{ pattern: /(\w*(?!\/)(\*\*)\w+(\*\*)\w*)|(\w*(__)\w+(__)\w*)/, type: 'macro', nearTop: true },
// italic
{ pattern: /(^.*(\*).+(\*)(?<!\/).*)$|^(.*(_).+(_).*)$/, type: 'macro', nearTop: true },
// lists
{ pattern: /^(?!-)(- \w*)(?<!-)|^(?!\/)(\* .*)/, type: 'meta.module', nearTop: true },
{ pattern: /^(?!!)(=|-){2,}(?<!>)$/, type: 'meta.module' },
// images
{ pattern: /!\[.+\]\(.+\)/, type: 'macro', nearTop: true },
// links
{ pattern: /\[.+\]\(.+\)/, type: 'macro', nearTop: true },
{ pattern: /(!)?\[.+\]\(.+\)/, type: 'keyword' },
// links 2
{ pattern: /\[.+\]\[.+\]/, type: 'macro', nearTop: true },
{ pattern: /\[.+\]\[.+\]/, type: 'keyword' },
// links 3
{ pattern: /\[.+\]:\s?<?http/, type: 'macro', nearTop: true },
{ pattern: /^\[.+\]:\s?(<)?(http)?/, type: 'keyword' },
// blockquotes
{ pattern: /^(> .*)+/, type: 'macro', nearTop: true },
// inline code
{ pattern: /.*`.+`.*/, type: 'meta.module', nearTop: true },
{ pattern: /^(> .*)+/, type: 'macro' },
// code block
{ pattern: /^```([A-Za-z0-9#_]+)?$/, type: 'keyword' },
// frontmatter
{ pattern: /^---$/, type: 'meta.module', nearTop: true },
];
3 changes: 2 additions & 1 deletion tests/cpp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ test('hello world', () => {
CSS: 0,
'C#': 0,
Dockerfile: 0,
Elixir: 0,
Go: 0,
HTML: 0,
Java: 0,
Javascript: 0,
Julia: 2,
Kotlin: 0,
Lua: 2,
Markdown: 0,
Pascal: 0,
PHP: 0,
Python: 0,
Expand All @@ -30,7 +32,6 @@ test('hello world', () => {
SQL: 0,
Unknown: 1,
YAML: 0,
Markdown: 0,
});
assert.equal(code.linesOfCode, 1);
});
Expand Down
3 changes: 2 additions & 1 deletion tests/cs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ test('hello world', () => {
'C#': 10,
CSS: 0,
Dockerfile: 0,
Elixir: 0,
Go: -39,
HTML: 0,
Java: -40,
Javascript: -40,
Julia: 5,
Kotlin: 0,
Lua: -20,
Markdown: 0,
Pascal: 0,
PHP: 0,
Python: 0,
Expand All @@ -31,7 +33,6 @@ test('hello world', () => {
SQL: 0,
Unknown: 1,
YAML: 0,
Markdown: 0,
});
assert.equal(code.linesOfCode, 2);
});
Expand Down
Loading

0 comments on commit 77a052d

Please sign in to comment.