Gisle Aune
4 years ago
commit
eed7ac843e
39 changed files with 8280 additions and 0 deletions
-
9.gitignore
-
157README.md
-
2compiler/.gitignore
-
312compiler/Cargo.lock
-
15compiler/Cargo.toml
-
145compiler/src/input.rs
-
6compiler/src/lib.rs
-
172compiler/src/main.rs
-
60compiler/src/manifest.rs
-
24compiler/src/output.rs
-
5374package-lock.json
-
37package.json
-
135rollup.config.js
-
306scripts/setupTypeScript.js
-
39src/ambient.d.ts
-
5src/client.js
-
141src/components/Page.svelte
-
65src/components/Selector.svelte
-
19src/routes/[name].json.js
-
192src/routes/[name].svelte
-
40src/routes/_error.svelte
-
20src/routes/_names.js
-
16src/routes/index.json.js
-
46src/routes/index.svelte
-
5src/rust/namegen-wasm/.gitignore
-
29src/rust/namegen-wasm/Cargo.toml
-
1src/rust/namegen-wasm/Web.toml
-
196src/rust/namegen-wasm/index.d.ts
-
64src/rust/namegen-wasm/index.js
-
338src/rust/namegen-wasm/package-lock.json
-
126src/rust/namegen-wasm/src/lib.rs
-
17src/server.js
-
86src/service-worker.js
-
33src/template.html
-
BINstatic/favicon.png
-
28static/global.css
-
BINstatic/logo-192.png
-
BINstatic/logo-512.png
-
20static/manifest.json
@ -0,0 +1,9 @@ |
|||
.DS_Store |
|||
/node_modules/ |
|||
/src/node_modules/@sapper/ |
|||
yarn-error.log |
|||
/__sapper__/ |
|||
.idea |
|||
.vscode |
|||
|
|||
*.wasm |
@ -0,0 +1,157 @@ |
|||
# sapper-template |
|||
|
|||
The default template for setting up a [Sapper](https://github.com/sveltejs/sapper) project. Can use either Rollup or webpack as bundler. |
|||
|
|||
|
|||
## Getting started |
|||
|
|||
|
|||
### Using `degit` |
|||
|
|||
To create a new Sapper project based on Rollup locally, run |
|||
|
|||
```bash |
|||
npx degit "sveltejs/sapper-template#rollup" my-app |
|||
``` |
|||
|
|||
For a webpack-based project, instead run |
|||
|
|||
```bash |
|||
npx degit "sveltejs/sapper-template#webpack" my-app |
|||
``` |
|||
|
|||
[`degit`](https://github.com/Rich-Harris/degit) is a scaffolding tool that lets you create a directory from a branch in a repository. |
|||
|
|||
Replace `my-app` with the path where you wish to create the project. |
|||
|
|||
|
|||
### Using GitHub templates |
|||
|
|||
Alternatively, you can create the new project as a GitHub repository using GitHub's template feature. |
|||
|
|||
Go to either [sapper-template-rollup](https://github.com/sveltejs/sapper-template-rollup) or [sapper-template-webpack](https://github.com/sveltejs/sapper-template-webpack) and click on "Use this template" to create a new project repository initialized by the template. |
|||
|
|||
|
|||
### Running the project |
|||
|
|||
Once you have created the project, install dependencies and run the project in development mode: |
|||
|
|||
```bash |
|||
cd my-app |
|||
npm install # or yarn |
|||
npm run dev |
|||
``` |
|||
|
|||
This will start the development server on [localhost:3000](http://localhost:3000). Open it and click around. |
|||
|
|||
You now have a fully functional Sapper project! To get started developing, consult [sapper.svelte.dev](https://sapper.svelte.dev). |
|||
|
|||
### Using TypeScript |
|||
|
|||
By default, the template uses plain JavaScript. If you wish to use TypeScript instead, you need some changes to the project: |
|||
|
|||
* Add `typescript` as well as typings as dependences in `package.json` |
|||
* Configure the bundler to use [`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess) and transpile the TypeScript code. |
|||
* Add a `tsconfig.json` file |
|||
* Update the project code to TypeScript |
|||
|
|||
The template comes with a script that will perform these changes for you by running |
|||
|
|||
```bash |
|||
node scripts/setupTypeScript.js |
|||
``` |
|||
|
|||
`@sapper` dependencies are resolved through `src/node_modules/@sapper`, which is created during the build. You therefore need to run or build the project once to avoid warnings about missing dependencies. |
|||
|
|||
The script does not support webpack at the moment. |
|||
|
|||
## Directory structure |
|||
|
|||
Sapper expects to find two directories in the root of your project — `src` and `static`. |
|||
|
|||
|
|||
### src |
|||
|
|||
The [src](src) directory contains the entry points for your app — `client.js`, `server.js` and (optionally) a `service-worker.js` — along with a `template.html` file and a `routes` directory. |
|||
|
|||
|
|||
#### src/routes |
|||
|
|||
This is the heart of your Sapper app. There are two kinds of routes — *pages*, and *server routes*. |
|||
|
|||
**Pages** are Svelte components written in `.svelte` files. When a user first visits the application, they will be served a server-rendered version of the route in question, plus some JavaScript that 'hydrates' the page and initialises a client-side router. From that point forward, navigating to other pages is handled entirely on the client for a fast, app-like feel. (Sapper will preload and cache the code for these subsequent pages, so that navigation is instantaneous.) |
|||
|
|||
**Server routes** are modules written in `.js` files, that export functions corresponding to HTTP methods. Each function receives Express `request` and `response` objects as arguments, plus a `next` function. This is useful for creating a JSON API, for example. |
|||
|
|||
There are three simple rules for naming the files that define your routes: |
|||
|
|||
* A file called `src/routes/about.svelte` corresponds to the `/about` route. A file called `src/routes/blog/[slug].svelte` corresponds to the `/blog/:slug` route, in which case `params.slug` is available to the route |
|||
* The file `src/routes/index.svelte` (or `src/routes/index.js`) corresponds to the root of your app. `src/routes/about/index.svelte` is treated the same as `src/routes/about.svelte`. |
|||
* Files and directories with a leading underscore do *not* create routes. This allows you to colocate helper modules and components with the routes that depend on them — for example you could have a file called `src/routes/_helpers/datetime.js` and it would *not* create a `/_helpers/datetime` route. |
|||
|
|||
|
|||
#### src/node_modules/images |
|||
|
|||
Images added to `src/node_modules/images` can be imported into your code using `import 'images/<filename>'`. They will be given a dynamically generated filename containing a hash, allowing for efficient caching and serving the images on a CDN. |
|||
|
|||
See [`index.svelte`](src/routes/index.svelte) for an example. |
|||
|
|||
|
|||
#### src/node_modules/@sapper |
|||
|
|||
This directory is managed by Sapper and generated when building. It contains all the code you import from `@sapper` modules. |
|||
|
|||
|
|||
### static |
|||
|
|||
The [static](static) directory contains static assets that should be served publicly. Files in this directory will be available directly under the root URL, e.g. an `image.jpg` will be available as `/image.jpg`. |
|||
|
|||
The default [service-worker.js](src/service-worker.js) will preload and cache these files, by retrieving a list of `files` from the generated manifest: |
|||
|
|||
```js |
|||
import { files } from '@sapper/service-worker'; |
|||
``` |
|||
|
|||
If you have static files you do not want to cache, you should exclude them from this list after importing it (and before passing it to `cache.addAll`). |
|||
|
|||
Static files are served using [sirv](https://github.com/lukeed/sirv). |
|||
|
|||
|
|||
## Bundler configuration |
|||
|
|||
Sapper uses Rollup or webpack to provide code-splitting and dynamic imports, as well as compiling your Svelte components. With webpack, it also provides hot module reloading. As long as you don't do anything daft, you can edit the configuration files to add whatever plugins you'd like. |
|||
|
|||
|
|||
## Production mode and deployment |
|||
|
|||
To start a production version of your app, run `npm run build && npm start`. This will disable live reloading, and activate the appropriate bundler plugins. |
|||
|
|||
You can deploy your application to any environment that supports Node 10 or above. As an example, to deploy to [Vercel Now](https://vercel.com) when using `sapper export`, run these commands: |
|||
|
|||
```bash |
|||
npm install -g vercel |
|||
vercel |
|||
``` |
|||
|
|||
If your app can't be exported to a static site, you can use the [vercel-sapper](https://github.com/thgh/vercel-sapper) builder. You can find instructions on how to do so in its [README](https://github.com/thgh/vercel-sapper#basic-usage). |
|||
|
|||
|
|||
## Using external components |
|||
|
|||
When using Svelte components installed from npm, such as [@sveltejs/svelte-virtual-list](https://github.com/sveltejs/svelte-virtual-list), Svelte needs the original component source (rather than any precompiled JavaScript that ships with the component). This allows the component to be rendered server-side, and also keeps your client-side app smaller. |
|||
|
|||
Because of that, it's essential that the bundler doesn't treat the package as an *external dependency*. You can either modify the `external` option under `server` in [rollup.config.js](rollup.config.js) or the `externals` option in [webpack.config.js](webpack.config.js), or simply install the package to `devDependencies` rather than `dependencies`, which will cause it to get bundled (and therefore compiled) with your app: |
|||
|
|||
```bash |
|||
npm install -D @sveltejs/svelte-virtual-list |
|||
``` |
|||
|
|||
## Troubleshooting |
|||
|
|||
Using Windows and WSL2? |
|||
|
|||
If your project lives outside the WSL root directory, [this limitation](https://github.com/microsoft/WSL/issues/4169) is known to cause live-reloading to fail. See [this issue](https://github.com/sveltejs/sapper/issues/1150) for details. |
|||
|
|||
## Bugs and feedback |
|||
|
|||
Sapper is in early development, and may have the odd rough edge here and there. Please be vocal over on the [Sapper issue tracker](https://github.com/sveltejs/sapper/issues). |
@ -0,0 +1,2 @@ |
|||
/target |
|||
/output/*.json |
@ -0,0 +1,312 @@ |
|||
# This file is automatically @generated by Cargo. |
|||
# It is not intended for manual editing. |
|||
[[package]] |
|||
name = "bumpalo" |
|||
version = "3.6.1" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" |
|||
|
|||
[[package]] |
|||
name = "cfg-if" |
|||
version = "1.0.0" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" |
|||
|
|||
[[package]] |
|||
name = "dtoa" |
|||
version = "0.4.7" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "88d7ed2934d741c6b37e33e3832298e8850b53fd2d2bea03873375596c7cea4e" |
|||
|
|||
[[package]] |
|||
name = "getrandom" |
|||
version = "0.1.16" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" |
|||
dependencies = [ |
|||
"cfg-if", |
|||
"js-sys", |
|||
"libc", |
|||
"wasi", |
|||
"wasm-bindgen", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "glob" |
|||
version = "0.3.0" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" |
|||
|
|||
[[package]] |
|||
name = "itoa" |
|||
version = "0.4.7" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" |
|||
|
|||
[[package]] |
|||
name = "js-sys" |
|||
version = "0.3.48" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "dc9f84f9b115ce7843d60706df1422a916680bfdfcbdb0447c5614ff9d7e4d78" |
|||
dependencies = [ |
|||
"wasm-bindgen", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "lazy_static" |
|||
version = "1.4.0" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" |
|||
|
|||
[[package]] |
|||
name = "libc" |
|||
version = "0.2.86" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" |
|||
|
|||
[[package]] |
|||
name = "linked-hash-map" |
|||
version = "0.5.4" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" |
|||
|
|||
[[package]] |
|||
name = "log" |
|||
version = "0.4.14" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" |
|||
dependencies = [ |
|||
"cfg-if", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "namegen" |
|||
version = "0.1.3" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "5e734aa49099b0a090acd6f8d0d4f7c408d41e2ce529bd1faae21ad4bab65ae3" |
|||
dependencies = [ |
|||
"rand", |
|||
"serde", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "namegen-compiler" |
|||
version = "0.1.0" |
|||
dependencies = [ |
|||
"glob", |
|||
"namegen", |
|||
"serde", |
|||
"serde_derive", |
|||
"serde_json", |
|||
"serde_yaml", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "ppv-lite86" |
|||
version = "0.2.10" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" |
|||
|
|||
[[package]] |
|||
name = "proc-macro2" |
|||
version = "1.0.24" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" |
|||
dependencies = [ |
|||
"unicode-xid", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "quote" |
|||
version = "1.0.9" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" |
|||
dependencies = [ |
|||
"proc-macro2", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "rand" |
|||
version = "0.7.3" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" |
|||
dependencies = [ |
|||
"getrandom", |
|||
"libc", |
|||
"rand_chacha", |
|||
"rand_core", |
|||
"rand_hc", |
|||
"rand_pcg", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "rand_chacha" |
|||
version = "0.2.2" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" |
|||
dependencies = [ |
|||
"ppv-lite86", |
|||
"rand_core", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "rand_core" |
|||
version = "0.5.1" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" |
|||
dependencies = [ |
|||
"getrandom", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "rand_hc" |
|||
version = "0.2.0" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" |
|||
dependencies = [ |
|||
"rand_core", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "rand_pcg" |
|||
version = "0.2.1" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" |
|||
dependencies = [ |
|||
"rand_core", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "ryu" |
|||
version = "1.0.5" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" |
|||
|
|||
[[package]] |
|||
name = "serde" |
|||
version = "1.0.123" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" |
|||
dependencies = [ |
|||
"serde_derive", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "serde_derive" |
|||
version = "1.0.123" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" |
|||
dependencies = [ |
|||
"proc-macro2", |
|||
"quote", |
|||
"syn", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "serde_json" |
|||
version = "1.0.64" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" |
|||
dependencies = [ |
|||
"itoa", |
|||
"ryu", |
|||
"serde", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "serde_yaml" |
|||
version = "0.8.17" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23" |
|||
dependencies = [ |
|||
"dtoa", |
|||
"linked-hash-map", |
|||
"serde", |
|||
"yaml-rust", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "syn" |
|||
version = "1.0.60" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" |
|||
dependencies = [ |
|||
"proc-macro2", |
|||
"quote", |
|||
"unicode-xid", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "unicode-xid" |
|||
version = "0.2.1" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" |
|||
|
|||
[[package]] |
|||
name = "wasi" |
|||
version = "0.9.0+wasi-snapshot-preview1" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" |
|||
|
|||
[[package]] |
|||
name = "wasm-bindgen" |
|||
version = "0.2.71" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "7ee1280240b7c461d6a0071313e08f34a60b0365f14260362e5a2b17d1d31aa7" |
|||
dependencies = [ |
|||
"cfg-if", |
|||
"wasm-bindgen-macro", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "wasm-bindgen-backend" |
|||
version = "0.2.71" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "5b7d8b6942b8bb3a9b0e73fc79b98095a27de6fa247615e59d096754a3bc2aa8" |
|||
dependencies = [ |
|||
"bumpalo", |
|||
"lazy_static", |
|||
"log", |
|||
"proc-macro2", |
|||
"quote", |
|||
"syn", |
|||
"wasm-bindgen-shared", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "wasm-bindgen-macro" |
|||
version = "0.2.71" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "e5ac38da8ef716661f0f36c0d8320b89028efe10c7c0afde65baffb496ce0d3b" |
|||
dependencies = [ |
|||
"quote", |
|||
"wasm-bindgen-macro-support", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "wasm-bindgen-macro-support" |
|||
version = "0.2.71" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "cc053ec74d454df287b9374ee8abb36ffd5acb95ba87da3ba5b7d3fe20eb401e" |
|||
dependencies = [ |
|||
"proc-macro2", |
|||
"quote", |
|||
"syn", |
|||
"wasm-bindgen-backend", |
|||
"wasm-bindgen-shared", |
|||
] |
|||
|
|||
[[package]] |
|||
name = "wasm-bindgen-shared" |
|||
version = "0.2.71" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1" |
|||
|
|||
[[package]] |
|||
name = "yaml-rust" |
|||
version = "0.4.5" |
|||
source = "registry+https://github.com/rust-lang/crates.io-index" |
|||
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" |
|||
dependencies = [ |
|||
"linked-hash-map", |
|||
] |
@ -0,0 +1,15 @@ |
|||
[package] |
|||
name = "namegen-compiler" |
|||
version = "0.1.0" |
|||
authors = ["gisle"] |
|||
edition = "2018" |
|||
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
|||
|
|||
[dependencies] |
|||
namegen = { version = "0.1.3", features = ["serde"] } |
|||
serde = { version = "1.0.115", features = ["derive"] } |
|||
serde_derive = "1.0.123" |
|||
serde_json = "1.0.64" |
|||
serde_yaml = "0.8.17" |
|||
glob = "0.3.0" |
@ -0,0 +1,145 @@ |
|||
use std::collections::BTreeMap;
|
|||
use namegen::{SampleSet, Sample};
|
|||
use crate::manifest::SourceManifest;
|
|||
|
|||
#[derive(Default)]
|
|||
pub struct SourceCollection {
|
|||
sources: BTreeMap<String, Source>
|
|||
}
|
|||
|
|||
impl SourceCollection {
|
|||
pub fn sources(&self) -> impl Iterator<Item = (&str, &Source)> {
|
|||
self.sources.iter().map(|(k, v)| (k.as_str(), v))
|
|||
}
|
|||
|
|||
pub fn source(&self, name: &str) -> Option<&Source> {
|
|||
self.sources.get(name)
|
|||
}
|
|||
|
|||
pub fn source_mut(&mut self, name: &str) -> &mut Source {
|
|||
if !self.sources.contains_key(name) {
|
|||
self.sources.insert(name.to_owned(), Source::default());
|
|||
}
|
|||
|
|||
self.sources.get_mut(name).unwrap()
|
|||
}
|
|||
|
|||
pub fn load_full_names(&mut self, data: &str, source: &SourceManifest) -> Result<(), &'static str> {
|
|||
for line in data.lines() {
|
|||
if line.len() < 1 {
|
|||
continue
|
|||
}
|
|||
|
|||
if let Some(prefix) = &source.comment_prefix {
|
|||
if line.starts_with(prefix) {
|
|||
continue;
|
|||
}
|
|||
}
|
|||
|
|||
for (i, token) in line.split(' ').enumerate() {
|
|||
if let Some(empty_token) = &source.empty_token {
|
|||
if token == empty_token.as_str() {
|
|||
continue;
|
|||
}
|
|||
}
|
|||
|
|||
let mut target = self.source_mut(&source.targets[i]);
|
|||
|
|||
let mut token = if source.to_lower {
|
|||
token.to_owned().to_lowercase()
|
|||
} else {
|
|||
token.to_owned()
|
|||
};
|
|||
|
|||
target.set_mut("list").add_sample(Sample::Word(token))
|
|||
}
|
|||
}
|
|||
|
|||
Ok(())
|
|||
}
|
|||
|
|||
pub fn load_labeled_groups(&mut self, data: &str, source: &SourceManifest) -> Result<(), &'static str> {
|
|||
let mut target_index = 0usize;
|
|||
let mut new_group = true;
|
|||
let mut current_key = String::new();
|
|||
|
|||
for line in data.lines() {
|
|||
if line.len() < 1 {
|
|||
new_group = true;
|
|||
continue;
|
|||
}
|
|||
|
|||
if let Some(separator) = &source.separator {
|
|||
if line.starts_with(separator) {
|
|||
target_index += 1;
|
|||
new_group;
|
|||
continue;
|
|||
}
|
|||
}
|
|||
|
|||
if let Some(prefix) = &source.comment_prefix {
|
|||
if line.starts_with(prefix) {
|
|||
continue;
|
|||
}
|
|||
}
|
|||
|
|||
let mut target = self.source_mut(&source.targets[target_index]);
|
|||
|
|||
if new_group {
|
|||
current_key = line.to_owned();
|
|||
let labels: Vec<String> = line.split(' ').map(|s| s.to_owned()).collect();
|
|||
|
|||
target.set_labeled_mut(line, labels.as_slice());
|
|||
new_group = false;
|
|||
|
|||
continue;
|
|||
}
|
|||
|
|||
let line = if source.to_lower {
|
|||
line.to_owned().to_lowercase()
|
|||
} else {
|
|||
line.to_owned()
|
|||
};
|
|||
|
|||
let tokens: Vec<String> = line.split(' ').map(|s| s.to_owned()).collect();
|
|||
let set = target.set_mut(current_key.as_str());
|
|||
|
|||
if tokens.len() != set.labels().len() {
|
|||
return Err("Token length != label count");
|
|||
}
|
|||
|
|||
set.add_sample(
|
|||
Sample::Tokens(tokens)
|
|||
);
|
|||
}
|
|||
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
|
|||
#[derive(Default)]
|
|||
pub struct Source {
|
|||
sets: BTreeMap<String, SampleSet>
|
|||
}
|
|||
|
|||
impl Source {
|
|||
pub fn sets(&self) -> impl Iterator<Item = &SampleSet> {
|
|||
self.sets.values()
|
|||
}
|
|||
|
|||
pub fn set_mut(&mut self, name: &str) -> &mut SampleSet {
|
|||
if !self.sets.contains_key(name) {
|
|||
self.sets.insert(name.to_owned(), SampleSet::new());
|
|||
}
|
|||
|
|||
self.sets.get_mut(name).unwrap()
|
|||
}
|
|||
|
|||
pub fn set_labeled_mut<S: AsRef<str>>(&mut self, name: &str, labels: &[S]) -> &mut SampleSet {
|
|||
if !self.sets.contains_key(name) {
|
|||
self.sets.insert(name.to_owned(), SampleSet::with_labels(labels));
|
|||
}
|
|||
|
|||
self.sets.get_mut(name).unwrap()
|
|||
}
|
|||
}
|
@ -0,0 +1,6 @@ |
|||
#[macro_use]
|
|||
extern crate serde_derive;
|
|||
|
|||
pub mod manifest;
|
|||
pub mod input;
|
|||
pub mod output;
|
@ -0,0 +1,172 @@ |
|||
use std::process;
|
|||
use std::env;
|
|||
use std::fs::{read_dir, File};
|
|||
use std::collections::HashMap;
|
|||
use namegen_compiler::manifest::Manifest;
|
|||
use std::env::join_paths;
|
|||
use std::path::Path;
|
|||
use namegen_compiler::input::SourceCollection;
|
|||
use glob::glob;
|
|||
use std::io::{Read};
|
|||
use namegen::{Name, NamePart};
|
|||
use namegen_compiler::output::{Output, Collection, CollectionItem};
|
|||
|
|||
const COMMON_FORMAT_NAMES: &'static [&str] = &[
|
|||
"full_name", "full_name:female", "full_name:male",
|
|||
];
|
|||
|
|||
fn main() {
|
|||
let source_dir = match env::var("SOURCE_DIR") {
|
|||
Ok(v) => v,
|
|||
Err(_) => {
|
|||
eprintln!("SOURCE_DIR missing");
|
|||
process::exit(1);
|
|||
}
|
|||
};
|
|||
|
|||
let dest_dir = match env::var("DESTINATION_DIR") {
|
|||
Ok(v) => v,
|
|||
Err(_) => {
|
|||
eprintln!("DESTINATION_DIR missing");
|
|||
process::exit(1);
|
|||
}
|
|||
};
|
|||
|
|||
let mut data = String::with_capacity(2048);
|
|||
let mut collection = Collection{items: Vec::new()};
|
|||
|
|||
for entry in read_dir(&source_dir).unwrap() {
|
|||
let entry = entry.unwrap();
|
|||
if !entry.file_type().unwrap().is_file() {
|
|||
continue;
|
|||
}
|
|||
|
|||
let path = Path::new(&source_dir).join(entry.file_name());
|
|||
eprintln!("Loading manifest {}", path.to_str().unwrap());
|
|||
let manifest: Manifest = serde_yaml::from_reader(File::open(path.clone()).unwrap()).unwrap();
|
|||
|
|||
let mut coll = SourceCollection::default();
|
|||
for source in manifest.sources.iter() {
|
|||
let path = path.with_file_name(&source.files);
|
|||
|
|||
for entry in glob(path.to_str().unwrap()).expect("Failed to read glob pattern") {
|
|||
let path = entry.expect("Invalid file entry.");
|
|||
|
|||
data.clear();
|
|||
File::open(path.clone()).unwrap().read_to_string(&mut data);
|
|||
|
|||
eprintln!("Loading source {} (size: {})", path.clone().to_str().unwrap(), data.len());
|
|||
|
|||
match source.kind.as_str() {
|
|||
"full_names" => { coll.load_full_names(&data, source).unwrap() }
|
|||
"labeled_groups" => { coll.load_labeled_groups(&data, source).unwrap() }
|
|||
_ => {
|
|||
eprintln!("Unknown source file kind {}", source.kind);
|
|||
process::exit(1);
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
let mut name = Name::new();
|
|||
|
|||
for part in manifest.parts.iter() {
|
|||
eprintln!("Building part {}...", &part.name);
|
|||
|
|||
let source = coll.source(&part.source).expect("Source not found.");
|
|||
let mut part = match part.kind.as_str() {
|
|||
"cfgrammar" => NamePart::new_cfgrammar(
|
|||
&part.name, &part.format_rules, &part.initial_tokens,
|
|||
part.rules.rlf, part.rules.ral,
|
|||
),
|
|||
"markov" => NamePart::new_markov(
|
|||
&part.name, &part.format_rules, &part.initial_tokens,
|
|||
part.rules.lrs, part.rules.lrm, part.rules.lre, part.rules.rlf,
|
|||
),
|
|||
"wordlist" => NamePart::new_wordlist(
|
|||
&part.name, &part.format_rules
|
|||
),
|
|||
|
|||
_ => {
|
|||
eprintln!("Unknown part kind {}", &part.kind);
|
|||
process::exit(1);
|
|||
},
|
|||
};
|
|||
|
|||
for set in source.sets() {
|
|||
if set.labels().len() == 0 {
|
|||
eprintln!("\tSample list: {} samples", set.samples().len());
|
|||
} else {
|
|||
eprintln!("\tSample group: {}", set.labels().join(" "));
|
|||
}
|
|||
part.learn(set).unwrap()
|
|||
}
|
|||
|
|||
name.add_part(part);
|
|||
}
|
|||
|
|||
for format in manifest.formats.iter() {
|
|||
name.add_format(&format.name, &format.template)
|
|||
}
|
|||
|
|||
let mut examples = HashMap::new();
|
|||
for format in name.formats() {
|
|||
if let Some(gen) = name.generate(format.name()) {
|
|||
examples.insert(format.name().to_owned(), Vec::with_capacity(40));
|
|||
|
|||
for (i, result) in gen.enumerate().take(40) {
|
|||
examples.get_mut(format.name()).unwrap().push(result);
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
eprint!("\n");
|
|||
eprintln!("Sample output");
|
|||
for format_name in COMMON_FORMAT_NAMES.iter() {
|
|||
if let Some(gen) = name.generate(format_name) {
|
|||
eprint!("\n ");
|
|||
|
|||
for (i, result) in gen.enumerate().take(72) {
|
|||
eprint!("{result:<width$} ", result = result, width = 19);
|
|||
if i > 0 && (i % 4 == 3) {
|
|||
eprint!("\n ");
|
|||
}
|
|||
}
|
|||
|
|||
eprint!("\n");
|
|||
}
|
|||
}
|
|||
|
|||
if let Some(gen) = name.generate("long_full_name:male") {
|
|||
eprint!("\n ");
|
|||
|
|||
for (i, result) in gen.enumerate().take(16) {
|
|||
eprint!("{result:<width$} ", result = result, width = 52);
|
|||
if i > 0 && (i % 2 == 1) {
|
|||
eprint!("\n ");
|
|||
}
|
|||
}
|
|||
|
|||
eprint!("\n");
|
|||
}
|
|||
|
|||
let path = Path::new(&dest_dir).join(format!("{}.json", &manifest.name));
|
|||
let mut output = Output{
|
|||
name: manifest.name.clone(),
|
|||
metadata: manifest.metadata.clone(),
|
|||
data: name,
|
|||
examples: examples,
|
|||
};
|
|||
|
|||
eprintln!("Writing {}", path.clone().to_str().unwrap());
|
|||
|
|||
serde_json::to_writer(File::create(path).expect("Opening output filed failed"), &output).expect("Writing output failed.");
|
|||
|
|||
collection.items.push(CollectionItem{name: output.name.clone(), metadata: output.metadata.clone()});
|
|||
|
|||
eprint!("\n\n");
|
|||
}
|
|||
|
|||
let path = Path::new(&dest_dir).join("_collection.json");
|
|||
serde_json::to_writer(File::create(path).expect("Opening output filed failed"), &collection).expect("Writing collection failed.");
|
|||
}
|
@ -0,0 +1,60 @@ |
|||
use std::collections::HashMap;
|
|||
use namegen::FormattingRule;
|
|||
|
|||
#[derive(Deserialize)]
|
|||
pub struct Manifest {
|
|||
pub name: String,
|
|||
#[serde(default)]
|
|||
pub metadata: HashMap<String, String>,
|
|||
pub sources: Vec<SourceManifest>,
|
|||
pub formats: Vec<FormatManifest>,
|
|||
pub parts: Vec<PartManifest>,
|
|||
}
|
|||
|
|||
#[derive(Deserialize)]
|
|||
pub struct PartManifest {
|
|||
pub name: String,
|
|||
pub kind: String,
|
|||
#[serde(default)]
|
|||
pub initial_tokens: Vec<String>,
|
|||
#[serde(default)]
|
|||
pub rules: PartManifestRule,
|
|||
pub source: String,
|
|||
#[serde(default)]
|
|||
pub format_rules: Vec<FormattingRule>,
|
|||
}
|
|||
|
|||
#[derive(Deserialize, Default)]
|
|||
pub struct PartManifestRule {
|
|||
#[serde(default)]
|
|||
pub rlf: bool,
|
|||
#[serde(default)]
|
|||
pub ral: bool,
|
|||
#[serde(default)]
|
|||
pub lrs: bool,
|
|||
#[serde(default)]
|
|||
pub lrm: bool,
|
|||
#[serde(default)]
|
|||
pub lre: bool,
|
|||
}
|
|||
|
|||
#[derive(Deserialize)]
|
|||
pub struct SourceManifest {
|
|||
pub files: String,
|
|||
pub kind: String,
|
|||
pub targets: Vec<String>,
|
|||
#[serde(default)]
|
|||
pub to_lower: bool,
|
|||
#[serde(default)]
|
|||
pub empty_token: Option<String>,
|
|||
#[serde(default)]
|
|||
pub separator: Option<String>,
|
|||
#[serde(default)]
|
|||
pub comment_prefix: Option<String>,
|
|||
}
|
|||
|
|||
#[derive(Deserialize)]
|
|||
pub struct FormatManifest {
|
|||
pub name: String,
|
|||
pub template: String,
|
|||
}
|
@ -0,0 +1,24 @@ |
|||
use std::collections::HashMap;
|
|||
use namegen::Name;
|
|||
|
|||
#[derive(Serialize)]
|
|||
#[serde(rename_all = "camelCase")]
|
|||
pub struct Output {
|
|||
pub name: String,
|
|||
pub metadata: HashMap<String, String>,
|
|||
pub data: Name,
|
|||
pub examples: HashMap<String, Vec<String>>,
|
|||
}
|
|||
|
|||
#[derive(Serialize)]
|
|||
#[serde(rename_all = "camelCase")]
|
|||
pub struct Collection {
|
|||
pub items: Vec<CollectionItem>,
|
|||
}
|
|||
|
|||
#[derive(Serialize)]
|
|||
#[serde(rename_all = "camelCase")]
|
|||
pub struct CollectionItem {
|
|||
pub name: String,
|
|||
pub metadata: HashMap<String, String>,
|
|||
}
|
5374
package-lock.json
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,37 @@ |
|||
{ |
|||
"name": "TODO", |
|||
"description": "TODO", |
|||
"version": "0.0.1", |
|||
"scripts": { |
|||
"dev": "sapper dev", |
|||
"build": "sapper build --legacy", |
|||
"export": "sapper export --legacy", |
|||
"start": "node __sapper__/build" |
|||
}, |
|||
"dependencies": { |
|||
"compression": "^1.7.1", |
|||
"polka": "next", |
|||
"sirv": "^1.0.0" |
|||
}, |
|||
"devDependencies": { |
|||
"@babel/core": "^7.0.0", |
|||
"@babel/plugin-syntax-dynamic-import": "^7.0.0", |
|||
"@babel/plugin-transform-runtime": "^7.0.0", |
|||
"@babel/preset-env": "^7.0.0", |
|||
"@babel/runtime": "^7.0.0", |
|||
"@rollup/plugin-babel": "^5.0.0", |
|||
"@rollup/plugin-commonjs": "^14.0.0", |
|||
"@rollup/plugin-json": "^4.1.0", |
|||
"@rollup/plugin-node-resolve": "^8.0.0", |
|||
"@rollup/plugin-replace": "^2.4.0", |
|||
"@rollup/plugin-url": "^5.0.0", |
|||
"@rollup/plugin-wasm": "^5.1.2", |
|||
"@wasm-tool/rollup-plugin-rust": "^1.0.5", |
|||
"capitalize": "^2.0.3", |
|||
"rollup": "^2.3.4", |
|||
"rollup-plugin-svelte": "^7.0.0", |
|||
"rollup-plugin-terser": "^7.0.0", |
|||
"sapper": "^0.28.0", |
|||
"svelte": "^3.17.3" |
|||
} |
|||
} |
@ -0,0 +1,135 @@ |
|||
import path from 'path'; |
|||
import resolve from '@rollup/plugin-node-resolve'; |
|||
import replace from '@rollup/plugin-replace'; |
|||
import json from '@rollup/plugin-json'; |
|||
import { wasm } from '@rollup/plugin-wasm'; |
|||
import commonjs from '@rollup/plugin-commonjs'; |
|||
import url from '@rollup/plugin-url'; |
|||
import svelte from 'rollup-plugin-svelte'; |
|||
import babel from '@rollup/plugin-babel'; |
|||
import { terser } from 'rollup-plugin-terser'; |
|||
import config from 'sapper/config/rollup.js'; |
|||
import pkg from './package.json'; |
|||
import rust from "@wasm-tool/rollup-plugin-rust"; |
|||
|
|||
const mode = process.env.NODE_ENV; |
|||
const dev = mode === 'development'; |
|||
const legacy = !!process.env.SAPPER_LEGACY_BUILD; |
|||
|
|||
const onwarn = (warning, onwarn) => |
|||
(warning.code === 'MISSING_EXPORT' && /'preload'/.test(warning.message)) || |
|||
(warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || |
|||
onwarn(warning); |
|||
|
|||
export default { |
|||
client: { |
|||
input: config.client.input(), |
|||
output: config.client.output(), |
|||
plugins: [ |
|||
replace({ |
|||
preventAssignment: true, |
|||
values:{ |
|||
'process.browser': true, |
|||
'process.env.NODE_ENV': JSON.stringify(mode) |
|||
}, |
|||
}), |
|||
wasm(), |
|||
svelte({ |
|||
compilerOptions: { |
|||
dev, |
|||
hydratable: true |
|||
} |
|||
}), |
|||
url({ |
|||
sourceDir: path.resolve(__dirname, 'src/node_modules/images'), |
|||
publicPath: '/client/' |
|||
}), |
|||
resolve({ |
|||
browser: true, |
|||
dedupe: ['svelte'] |
|||
}), |
|||
commonjs(), |
|||
json({compact: true}), |
|||
rust(), |
|||
|
|||
legacy && babel({ |
|||
extensions: ['.js', '.mjs', '.html', '.svelte'], |
|||
babelHelpers: 'runtime', |
|||
exclude: ['node_modules/@babel/**'], |
|||
presets: [ |
|||
['@babel/preset-env', { |
|||
targets: '> 0.25%, not dead' |
|||
}] |
|||
], |
|||
plugins: [ |
|||
'@babel/plugin-syntax-dynamic-import', |
|||
['@babel/plugin-transform-runtime', { |
|||
useESModules: true |
|||
}] |
|||
] |
|||
}), |
|||
|
|||
!dev && terser({ |
|||
module: true |
|||
}) |
|||
], |
|||
|
|||
preserveEntrySignatures: false, |
|||
onwarn, |
|||
}, |
|||
|
|||
server: { |
|||
input: config.server.input(), |
|||
output: config.server.output(), |
|||
plugins: [ |
|||
replace({ |
|||
preventAssignment: true, |
|||
values:{ |
|||
'process.browser': false, |
|||
'process.env.NODE_ENV': JSON.stringify(mode) |
|||
}, |
|||
}), |
|||
svelte({ |
|||
compilerOptions: { |
|||
dev, |
|||
generate: 'ssr', |
|||
hydratable: true |
|||
}, |
|||
emitCss: false |
|||
}), |
|||
url({ |
|||
sourceDir: path.resolve(__dirname, 'src/node_modules/images'), |
|||
publicPath: '/client/', |
|||
emitFiles: false // already emitted by client build
|
|||
}), |
|||
resolve({ |
|||
dedupe: ['svelte'] |
|||
}), |
|||
rust(), |
|||
commonjs() |
|||
], |
|||
external: Object.keys(pkg.dependencies).concat(require('module').builtinModules), |
|||
preserveEntrySignatures: 'strict', |
|||
onwarn, |
|||
}, |
|||
|
|||
serviceworker: { |
|||
input: config.serviceworker.input(), |
|||
output: config.serviceworker.output(), |
|||
plugins: [ |
|||
resolve(), |
|||
replace({ |
|||
preventAssignment: true, |
|||
values:{ |
|||
'process.browser': true, |
|||
'process.env.NODE_ENV': JSON.stringify(mode) |
|||
}, |
|||
}), |
|||
commonjs(), |
|||
rust(), |
|||
!dev && terser() |
|||
], |
|||
preserveEntrySignatures: false, |
|||
onwarn, |
|||
} |
|||
}; |
@ -0,0 +1,306 @@ |
|||
/** |
|||
* Run this script to convert the project to TypeScript. This is only guaranteed to work |
|||
* on the unmodified default template; if you have done code changes you are likely need |
|||
* to touch up the generated project manually. |
|||
*/ |
|||
|
|||
// @ts-check
|
|||
const fs = require('fs'); |
|||
const path = require('path'); |
|||
const { argv } = require('process'); |
|||
|
|||
const projectRoot = argv[2] || path.join(__dirname, '..'); |
|||
|
|||
const isRollup = fs.existsSync(path.join(projectRoot, "rollup.config.js")); |
|||
|
|||
function warn(message) { |
|||
console.warn('Warning: ' + message); |
|||
} |
|||
|
|||
function replaceInFile(fileName, replacements) { |
|||
if (fs.existsSync(fileName)) { |
|||
let contents = fs.readFileSync(fileName, 'utf8'); |
|||
let hadUpdates = false; |
|||
|
|||
replacements.forEach(([from, to]) => { |
|||
const newContents = contents.replace(from, to); |
|||
|
|||
const isAlreadyApplied = typeof to !== 'string' || contents.includes(to); |
|||
|
|||
if (newContents !== contents) { |
|||
contents = newContents; |
|||
hadUpdates = true; |
|||
} else if (!isAlreadyApplied) { |
|||
warn(`Wanted to update "${from}" in ${fileName}, but did not find it.`); |
|||
} |
|||
}); |
|||
|
|||
if (hadUpdates) { |
|||
fs.writeFileSync(fileName, contents); |
|||
} else { |
|||
console.log(`${fileName} had already been updated.`); |
|||
} |
|||
} else { |
|||
warn(`Wanted to update ${fileName} but the file did not exist.`); |
|||
} |
|||
} |
|||
|
|||
function createFile(fileName, contents) { |
|||
if (fs.existsSync(fileName)) { |
|||
warn(`Wanted to create ${fileName}, but it already existed. Leaving existing file.`); |
|||
} else { |
|||
fs.writeFileSync(fileName, contents); |
|||
} |
|||
} |
|||
|
|||
function addDepsToPackageJson() { |
|||
const pkgJSONPath = path.join(projectRoot, 'package.json'); |
|||
const packageJSON = JSON.parse(fs.readFileSync(pkgJSONPath, 'utf8')); |
|||
packageJSON.devDependencies = Object.assign(packageJSON.devDependencies, { |
|||
...(isRollup ? { '@rollup/plugin-typescript': '^6.0.0' } : { 'ts-loader': '^8.0.4' }), |
|||
'@tsconfig/svelte': '^1.0.10', |
|||
'@types/compression': '^1.7.0', |
|||
'@types/node': '^14.11.1', |
|||
'@types/polka': '^0.5.1', |
|||
'svelte-check': '^1.0.46', |
|||
'svelte-preprocess': '^4.3.0', |
|||
tslib: '^2.0.1', |
|||
typescript: '^4.0.3' |
|||
}); |
|||
|
|||
// Add script for checking
|
|||
packageJSON.scripts = Object.assign(packageJSON.scripts, { |
|||
validate: 'svelte-check --ignore src/node_modules/@sapper' |
|||
}); |
|||
|
|||
// Write the package JSON
|
|||
fs.writeFileSync(pkgJSONPath, JSON.stringify(packageJSON, null, ' ')); |
|||
} |
|||
|
|||
function changeJsExtensionToTs(dir) { |
|||
const elements = fs.readdirSync(dir, { withFileTypes: true }); |
|||
|
|||
for (let i = 0; i < elements.length; i++) { |
|||
if (elements[i].isDirectory()) { |
|||
changeJsExtensionToTs(path.join(dir, elements[i].name)); |
|||
} else if (elements[i].name.match(/^[^_]((?!json).)*js$/)) { |
|||
fs.renameSync(path.join(dir, elements[i].name), path.join(dir, elements[i].name).replace('.js', '.ts')); |
|||
} |
|||
} |
|||
} |
|||
|
|||
function updateSingleSvelteFile({ view, vars, contextModule }) { |
|||
replaceInFile(path.join(projectRoot, 'src', `${view}.svelte`), [ |
|||
[/(?:<script)(( .*?)*?)>/gm, (m, attrs) => `<script${attrs}${!attrs.includes('lang="ts"') ? ' lang="ts"' : ''}>`], |
|||
...(vars ? vars.map(({ name, type }) => [`export let ${name};`, `export let ${name}: ${type};`]) : []), |
|||
...(contextModule ? contextModule.map(({ js, ts }) => [js, ts]) : []) |
|||
]); |
|||
} |
|||
|
|||
// Switch the *.svelte file to use TS
|
|||
function updateSvelteFiles() { |
|||
[ |
|||
{ |
|||
view: 'components/Nav', |
|||
vars: [{ name: 'segment', type: 'string' }] |
|||
}, |
|||
{ |
|||
view: 'routes/_layout', |
|||
vars: [{ name: 'segment', type: 'string' }] |
|||
}, |
|||
{ |
|||
view: 'routes/_error', |
|||
vars: [ |
|||
{ name: 'status', type: 'number' }, |
|||
{ name: 'error', type: 'Error' } |
|||
] |
|||
}, |
|||
{ |
|||
view: 'routes/blog/index', |
|||
vars: [{ name: 'posts', type: '{ slug: string; title: string, html: any }[]' }], |
|||
contextModule: [ |
|||
{ |
|||
js: '.then(r => r.json())', |
|||
ts: '.then((r: { json: () => any; }) => r.json())' |
|||
}, |
|||
{ |
|||
js: '.then(posts => {', |
|||
ts: '.then((posts: { slug: string; title: string, html: any }[]) => {' |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
view: 'routes/blog/[slug]', |
|||
vars: [{ name: 'post', type: '{ slug: string; title: string, html: any }' }] |
|||
} |
|||
].forEach(updateSingleSvelteFile); |
|||
} |
|||
|
|||
function updateRollupConfig() { |
|||
// Edit rollup config
|
|||
replaceInFile(path.join(projectRoot, 'rollup.config.js'), [ |
|||
// Edit imports
|
|||
[ |
|||
/'rollup-plugin-terser';\n(?!import sveltePreprocess)/, |
|||
`'rollup-plugin-terser';
|
|||
import sveltePreprocess from 'svelte-preprocess'; |
|||
import typescript from '@rollup/plugin-typescript'; |
|||
`
|
|||
], |
|||
// Edit inputs
|
|||
[ |
|||
/(?<!THIS_IS_UNDEFINED[^\n]*\n\s*)onwarn\(warning\);/, |
|||
`(warning.code === 'THIS_IS_UNDEFINED') ||\n\tonwarn(warning);` |
|||
], |
|||
[/input: config.client.input\(\)(?!\.replace)/, `input: config.client.input().replace(/\\.js$/, '.ts')`], |
|||
[ |
|||
/input: config.server.input\(\)(?!\.replace)/, |
|||
`input: { server: config.server.input().server.replace(/\\.js$/, ".ts") }` |
|||
], |
|||
[ |
|||
/input: config.serviceworker.input\(\)(?!\.replace)/, |
|||
`input: config.serviceworker.input().replace(/\\.js$/, '.ts')` |
|||
], |
|||
// Add preprocess
|
|||
[/compilerOptions/g, 'preprocess: sveltePreprocess({ sourceMap: dev }),\n\t\t\t\tcompilerOptions'], |
|||
// Add TypeScript
|
|||
[/commonjs\(\)(?!,\n\s*typescript)/g, 'commonjs(),\n\t\t\ttypescript({ sourceMap: dev })'] |
|||
]); |
|||
} |
|||
|
|||
function updateWebpackConfig() { |
|||
// Edit webpack config
|
|||
replaceInFile(path.join(projectRoot, 'webpack.config.js'), [ |
|||
// Edit imports
|
|||
[ |
|||
/require\('webpack-modules'\);\n(?!const sveltePreprocess)/, |
|||
`require('webpack-modules');\nconst sveltePreprocess = require('svelte-preprocess');\n` |
|||
], |
|||
// Edit extensions
|
|||
[ |
|||
/\['\.mjs', '\.js', '\.json', '\.svelte', '\.html'\]/, |
|||
`['.mjs', '.js', '.ts', '.json', '.svelte', '.html']` |
|||
], |
|||
// Edit entries
|
|||
[ |
|||
/entry: config\.client\.entry\(\)/, |
|||
`entry: { main: config.client.entry().main.replace(/\\.js$/, '.ts') }` |
|||
], |
|||
[ |
|||
/entry: config\.server\.entry\(\)/, |
|||
`entry: { server: config.server.entry().server.replace(/\\.js$/, '.ts') }` |
|||
], |
|||
[ |
|||
/entry: config\.serviceworker\.entry\(\)/, |
|||
`entry: { 'service-worker': config.serviceworker.entry()['service-worker'].replace(/\\.js$/, '.ts') }` |
|||
], |
|||
// Add preprocess to the svelte config, this is tricky because there's no easy signifier.
|
|||
// Instead we look for 'hydratable: true,'
|
|||
[ |
|||
/hydratable: true(?!,\n\s*preprocess)/g, |
|||
'hydratable: true,\n\t\t\t\t\t\t\tpreprocess: sveltePreprocess({ sourceMap: dev })' |
|||
], |
|||
// Add TypeScript rules for client and server
|
|||
[ |
|||
/module: {\n\s*rules: \[\n\s*(?!{\n\s*test: \/\\\.ts\$\/)/g, |
|||
`module: {\n\t\t\trules: [\n\t\t\t\t{\n\t\t\t\t\ttest: /\\.ts$/,\n\t\t\t\t\tloader: 'ts-loader'\n\t\t\t\t},\n\t\t\t\t` |
|||
], |
|||
// Add TypeScript rules for serviceworker
|
|||
[ |
|||
/output: config\.serviceworker\.output\(\),\n\s*(?!module)/, |
|||
`output: config.serviceworker.output(),\n\t\tmodule: {\n\t\t\trules: [\n\t\t\t\t{\n\t\t\t\t\ttest: /\\.ts$/,\n\t\t\t\t\tloader: 'ts-loader'\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t` |
|||
], |
|||
// Edit outputs
|
|||
[ |
|||
/output: config\.serviceworker\.output\(\),\n\s*(?!resolve)/, |
|||
`output: config.serviceworker.output(),\n\t\tresolve: { extensions: ['.mjs', '.js', '.ts', '.json'] },\n\t\t` |
|||
] |
|||
]); |
|||
} |
|||
|
|||
function updateServiceWorker() { |
|||
replaceInFile(path.join(projectRoot, 'src', 'service-worker.ts'), [ |
|||
[`shell.concat(files);`, `(shell as string[]).concat(files as string[]);`], |
|||
[`self.skipWaiting();`, `((self as any) as ServiceWorkerGlobalScope).skipWaiting();`], |
|||
[`self.clients.claim();`, `((self as any) as ServiceWorkerGlobalScope).clients.claim();`], |
|||
[`fetchAndCache(request)`, `fetchAndCache(request: Request)`], |
|||
[`self.addEventListener('activate', event =>`, `self.addEventListener('activate', (event: ExtendableEvent) =>`], |
|||
[`self.addEventListener('install', event =>`, `self.addEventListener('install', (event: ExtendableEvent) =>`], |
|||
[`addEventListener('fetch', event =>`, `addEventListener('fetch', (event: FetchEvent) =>`], |
|||
]); |
|||
} |
|||
|
|||
function createTsConfig() { |
|||
const tsconfig = `{
|
|||
"extends": "@tsconfig/svelte/tsconfig.json", |
|||
"compilerOptions": { |
|||
"lib": ["DOM", "ES2017", "WebWorker"] |
|||
}, |
|||
"include": ["src/**/*", "src/node_modules/**/*"], |
|||
"exclude": ["node_modules/*", "__sapper__/*", "static/*"] |
|||
}`;
|
|||
|
|||
createFile(path.join(projectRoot, 'tsconfig.json'), tsconfig); |
|||
} |
|||
|
|||
// Adds the extension recommendation
|
|||
function configureVsCode() { |
|||
const dir = path.join(projectRoot, '.vscode'); |
|||
|
|||
if (!fs.existsSync(dir)) { |
|||
fs.mkdirSync(dir); |
|||
} |
|||
|
|||
createFile(path.join(projectRoot, '.vscode', 'extensions.json'), `{"recommendations": ["svelte.svelte-vscode"]}`); |
|||
} |
|||
|
|||
function deleteThisScript() { |
|||
fs.unlinkSync(path.join(__filename)); |
|||
|
|||
// Check for Mac's DS_store file, and if it's the only one left remove it
|
|||
const remainingFiles = fs.readdirSync(path.join(__dirname)); |
|||
if (remainingFiles.length === 1 && remainingFiles[0] === '.DS_store') { |
|||
fs.unlinkSync(path.join(__dirname, '.DS_store')); |
|||
} |
|||
|
|||
// Check if the scripts folder is empty
|
|||
if (fs.readdirSync(path.join(__dirname)).length === 0) { |
|||
// Remove the scripts folder
|
|||
fs.rmdirSync(path.join(__dirname)); |
|||
} |
|||
} |
|||
|
|||
console.log(`Adding TypeScript with ${isRollup ? "Rollup" : "webpack" }...`); |
|||
|
|||
addDepsToPackageJson(); |
|||
|
|||
changeJsExtensionToTs(path.join(projectRoot, 'src')); |
|||
|
|||
updateSvelteFiles(); |
|||
|
|||
if (isRollup) { |
|||
updateRollupConfig(); |
|||
} else { |
|||
updateWebpackConfig(); |
|||
} |
|||
|
|||
updateServiceWorker(); |
|||
|
|||
createTsConfig(); |
|||
|
|||
configureVsCode(); |
|||
|
|||
// Delete this script, but not during testing
|
|||
if (!argv[2]) { |
|||
deleteThisScript(); |
|||
} |
|||
|
|||
console.log('Converted to TypeScript.'); |
|||
|
|||
if (fs.existsSync(path.join(projectRoot, 'node_modules'))) { |
|||
console.log(`
|
|||
Next: |
|||
1. run 'npm install' again to install TypeScript dependencies |
|||
2. run 'npm run build' for the @sapper imports in your project to work |
|||
`);
|
|||
} |
@ -0,0 +1,39 @@ |
|||
/** |
|||
* These declarations tell TypeScript that we allow import of images, e.g. |
|||
* ```
|
|||
<script lang='ts'> |
|||
import successkid from 'images/successkid.jpg'; |
|||
</script> |
|||
|
|||
<img src="{successkid}"> |
|||
```
|
|||
*/ |
|||
declare module "*.gif" { |
|||
const value: string; |
|||
export = value; |
|||
} |
|||
|
|||
declare module "*.jpg" { |
|||
const value: string; |
|||
export = value; |
|||
} |
|||
|
|||
declare module "*.jpeg" { |
|||
const value: string; |
|||
export = value; |
|||
} |
|||
|
|||
declare module "*.png" { |
|||
const value: string; |
|||
export = value; |
|||
} |
|||
|
|||
declare module "*.svg" { |
|||
const value: string; |
|||
export = value; |
|||
} |
|||
|
|||
declare module "*.webp" { |
|||
const value: string; |
|||
export = value; |
|||
} |
@ -0,0 +1,5 @@ |
|||
import * as sapper from '@sapper/app'; |
|||
|
|||
sapper.start({ |
|||
target: document.querySelector('#sapper') |
|||
}); |
@ -0,0 +1,141 @@ |
|||
<script> |
|||
export let collection = {items: []}; |
|||
export let narrow = false; |
|||
export let selected = ""; |
|||
|
|||
let categories = []; |
|||
$: { |
|||
categories.splice(0); |
|||
|
|||
for (const item of collection.items) { |
|||
let category = categories.find(c => c.name === item.metadata.category); |
|||
if (category == null) { |
|||
category = {name: item.metadata.category, list: []} |
|||
categories.push(category); |
|||
} |
|||
|
|||
category.list.push({id: item.name, label: item.metadata.name, color: item.metadata.color || "#ffffff"}); |
|||
} |
|||
|
|||
categories.sort((a,b) => { |
|||
if (a.list.length !== b.list.length) { |
|||
return b.list.length - a.list.length; |
|||
} else { |
|||
return b.name.localeCompare(a.name); |
|||
} |
|||
}); |
|||
for (const category of categories) { |
|||
category.list.sort((a,b) => a.id.localeCompare(b.id)); |
|||
} |
|||
} |
|||
</script> |
|||
|
|||
<div class="layout"> |
|||
<div class="page" class:narrow> |
|||
<slot></slot> |
|||
</div> |
|||
<div class="menu"> |
|||
<h1> |
|||
<a class:selected={selected === "INDEX"} href="/">n.vmaple.dev</a> |
|||
</h1> |
|||
{#each categories as category (category.name)} |
|||
<h2>{category.name}</h2> |
|||
{#each category.list as item (item.id)} |
|||
<span style="--color: {item.color}"> |
|||
<a class:selected={selected === item.id} href="/{item.id}">{item.label}</a> |
|||
</span> |
|||
{/each} |
|||
{/each} |
|||
</div> |
|||
</div> |
|||
|
|||
<style> |
|||
div.menu { |
|||
position: fixed; |
|||
left: 0; |
|||
top: 0; |
|||
height: 100%; |
|||
overflow-y: auto; |
|||
box-sizing: border-box; |
|||
padding: 0em; |
|||
background: #222; |
|||
border: 1px solid #000; |
|||
width: 12em; |
|||
} |
|||
|
|||
div.menu h1 { |
|||
font-size: 1.5em; |
|||
text-align: center; |
|||
padding: 0.5em 0 0 0; |
|||
} |
|||
div.menu h1 a { |
|||
line-height: 1.5em; |
|||
} |
|||
|
|||
div.menu h2 { |
|||
font-size: 1.15em; |
|||
color: #555; |
|||
padding: 0.5em 1ch 0 1ch; |
|||
margin: 0; |
|||
} |
|||
|
|||
div.menu a { |
|||
display: block; |
|||
padding: 0.125em 2ch; |
|||
line-height: 1.5em; |
|||
text-decoration: none; |
|||
font-size: 0.9em; |
|||
color: #aaa; |
|||
} |
|||
div.menu a.selected { |
|||
background: #2a2a2a; |
|||
color: var(--color); |
|||
padding-left: 2.5ch; |
|||
} |
|||
div.menu a:hover { |
|||
background: #191919; |
|||
} |
|||
|
|||
div.layout { |
|||
padding-left: 12em; |
|||
} |
|||
|
|||
div.page { |
|||
width: 120ch; |
|||
max-width: 90%; |
|||
box-sizing: border-box; |
|||
margin: 2em auto; |
|||
background: #222; |
|||
border: 1px solid #000; |
|||
border-radius: 1em; |
|||
padding: 1em; |
|||
text-align: center; |
|||
} |
|||
div.page.narrow { |
|||
width: 75ch; |
|||
} |
|||
|
|||
@media screen and (max-width: 720px) { |
|||
div.layout { |
|||
padding-left: 0; |
|||
} |
|||
|
|||
div.menu { |
|||
position: initial; |
|||
width: 32ch; |
|||
max-width: 90%; |
|||
margin: 2em auto; |
|||
background: #222; |
|||
border: 1px solid #000; |
|||
border-radius: 1em; |
|||
padding: 1em; |
|||
text-align: center; |
|||
} |
|||
|
|||
div.menu a.selected { |
|||
padding-left: 2ch; |
|||
font-weight: 600; |
|||
} |
|||
} |
|||
|
|||
</style> |
@ -0,0 +1,65 @@ |
|||
<script> |
|||
/** @type {{value: string, text: string}[]} */ |
|||
export let options = [] |
|||
export let value = ""; |
|||
export let label = "Select"; |
|||
export let prefer = null; |
|||
export let color = "#ffffff"; |
|||
export let bgcolor = "#444444"; |
|||
|
|||
$: { |
|||
if (options.length > 0 && !options.find(o => o.value === value)) { |
|||
if (prefer != null) { |
|||
const preferred = options.find(o => o.value.includes(prefer)); |
|||
if (preferred != null) { |
|||
value = preferred.value; |
|||
} else { |
|||
value = options[0].value; |
|||
} |
|||
} else { |
|||
value = options[0].value; |
|||
} |
|||
} |
|||
} |
|||
</script> |
|||
|
|||
<div class="selector" style="--color: {color || "#ffcc11"}; --bgcolor: {bgcolor || "#776655"}"> |
|||
<h2>{label}</h2> |
|||
{#each options as option (option.value)} |
|||
<div on:click={() => value = option.value} class="option" class:selected={option.value === value}>{option.text}</div> |
|||
{/each} |
|||
</div> |
|||
|
|||
<style> |
|||
.selector { |
|||
margin: auto; |
|||
} |
|||
|
|||
h2 { |
|||
font-size: 1.25em; |
|||
margin-top: 1em; |
|||
} |
|||
|
|||
.option { |
|||
display: inline-block; |
|||
margin: 0; |
|||
padding: 0.5em 1.5ch; |
|||
line-height: 1.1em; |
|||
background: #333; |
|||
border: 1px solid #000; |
|||
cursor: pointer; |
|||
} |
|||
.option.selected { |
|||
color: var(--color); |
|||
background-color: var(--bgcolor); |
|||
} |
|||
.option:first-of-type { |
|||
border-top-left-radius: 0.5em; |
|||
border-bottom-left-radius: 0.5em; |
|||
} |
|||
.option:last-of-type { |
|||
border-top-right-radius: 0.5em; |
|||
border-bottom-right-radius: 0.5em; |
|||
} |
|||
|
|||
</style> |
@ -0,0 +1,19 @@ |
|||
import names, {collection} from './_names.js'; |
|||
|
|||
export function get(req, res) { |
|||
const name = names.find(n => n.name === req.params.name); |
|||
if (name == null) { |
|||
res.writeHead(404, { |
|||
'Content-Type': 'application/json' |
|||
}); |
|||
|
|||
res.end(JSON.stringify({code: 404, error: "Not found"})); |
|||
return; |
|||
} |
|||
|
|||
res.writeHead(200, { |
|||
'Content-Type': 'application/json' |
|||
}); |
|||
|
|||
res.end(JSON.stringify({name, collection})); |
|||
} |
@ -0,0 +1,192 @@ |
|||
<script context="module"> |
|||
export function preload({params}) { |
|||
return this.fetch(`${params.name}.json`).then(r => r.json()).then(({name, collection, error}) => { |
|||
return { name, collection }; |
|||
}); |
|||
} |
|||
</script> |
|||
|
|||
<script> |
|||
import { onMount } from "svelte"; |
|||
import capitalize from "capitalize"; |
|||
|
|||
import Page from "../components/Page.svelte"; |
|||
import Selector from "../components/Selector.svelte"; |
|||
|
|||
export let name; |
|||
export let collection; |
|||
|
|||
/** @type {import("../rust/namegen-wasm").default} */ |
|||
let generator = null; |
|||
let selectedSuffix = ""; |
|||
let selectedFormat = ""; |
|||
let generatedFormat = ""; |
|||
let generatedSuffix = ""; |
|||
let result = "" |
|||
let failed = false; |
|||
let buffer = []; |
|||
let formats = []; |
|||
let suffixes = []; |
|||
|
|||
let loaded = ""; |
|||
let mounted = false; |
|||
|
|||
function reload() { |
|||
loaded = name.name; |
|||
|
|||
import("../rust/namegen-wasm").then(({load, default: NameGenerator}) => { |
|||
return load().then(() => { |
|||
console.log("Setting up", name.name); |
|||
generator = new NameGenerator(name.data); |
|||
}) |
|||
}).catch(() => { |
|||
failed = true; |
|||
}).finally(() => { |
|||
loaded = name.name; |
|||
}) |
|||
} |
|||
|
|||
function generate() { |
|||
if (loaded !== name.name) { |
|||
return; |
|||
} |
|||
|
|||
let format = `${selectedFormat}:${selectedSuffix}`; |
|||
if (!name.data.formats.find(f => f.name === format)) { |
|||
format = selectedFormat; |
|||
} |
|||
if (!name.data.formats.find(f => f.name === format)) { |
|||
return; |
|||
} |
|||
|
|||
if (generator !== null) { |
|||
if (buffer.length === 0) { |
|||
console.time("Generate 64 names"); |
|||
buffer = generator.generateMany(format, 64); |
|||
console.timeEnd("Generate 64 names"); |
|||
} |
|||
|
|||
result = buffer.pop(); |
|||
} else { |
|||
result = name.examples[format][Math.floor(Math.random() * name.examples[format].length)] |
|||
} |
|||
|
|||
generatedFormat = selectedFormat; |
|||
generatedSuffix = selectedSuffix; |
|||
} |
|||
|
|||
onMount(() => { |
|||
mounted = true; |
|||
}) |
|||
|
|||
$: { |
|||
if (mounted && loaded !== name.name) { |
|||
generator = null; |
|||
buffer = []; |
|||
generatedFormat = ""; |
|||
generatedSuffix = ""; |
|||
selectedFormat = ""; |
|||
selectedSuffix = ""; |
|||
reload(); |
|||
} |
|||
} |
|||
|
|||
$: { |
|||
if (mounted && (selectedFormat !== generatedFormat || selectedSuffix !== generatedSuffix)) { |
|||
buffer = []; |
|||
generate(); |
|||
} |
|||
} |
|||
|
|||
$: { |
|||
const formatNames = name.data.formats.map(f => f.name.split(":")[0]).filter((f, i, a) => !a.slice(0, i).includes(f)); |
|||
const suffixNames = name.data.formats.map(s => s.name.split(":")[1]).filter(s => s).filter((s, i, a) => !a.slice(0, i).includes(s)); |
|||
const formatLabels = formatNames.map(n => name.metadata[`formatLabel_${n}`] || n.split("_").map(t => capitalize(t)).join(" ")); |
|||
const suffixLabels = suffixNames.map(n => name.metadata[`formatSuffixLabel_${n}`] || n.split("_").map(t => capitalize(t)).join(" ")); |
|||
|
|||
formats = formatNames.map((n, i) => ({value: n, text: formatLabels[i]})); |
|||
suffixes = suffixNames.map((n, i) => ({value: n, text: suffixLabels[i]})); |
|||
} |
|||
</script> |
|||
|
|||
<svelte:head> |
|||
<title>{name.metadata.name} - Namegen 5</title> |
|||
</svelte:head> |
|||
<Page collection={collection} selected={name.name}> |
|||
<div class="name"> |
|||
<h1>{name.metadata.name}</h1> |
|||
<div class="cateogry">{name.metadata.category}</div> |
|||
<div class="description">{name.metadata.description || ""}</div> |
|||
<div style="color: {name.metadata.color || "#eeeeee"}" class:long={result.length > 25} class="result">{result}</div> |
|||
{#if (failed)} |
|||
<div class="failed">Your browser does not support Webassembly, the generator will only pick one out of 30 examples.</div> |
|||
{/if} |
|||
{#if suffixes.length > 0} |
|||
<Selector color={name.metadata.color} bgcolor={name.metadata.bgcolor} label={name.metadata.formatSuffixType} options={suffixes} bind:value={selectedSuffix} /> |
|||
{/if} |
|||
<Selector color={name.metadata.color} bgcolor={name.metadata.bgcolor} label="Format" prefer="full_" options={formats} bind:value={selectedFormat} /> |
|||
|
|||
<button disabled={(loaded !== name.name) && !failed} on:click={generate}>Generate</button> |
|||
<div class="footnote">{name.metadata.footnote || ""}</div> |
|||
</div> |
|||
</Page> |
|||
|
|||
<style> |
|||
h1 { |
|||
text-align: center; |
|||
margin-bottom: 0; |
|||
line-height: 1em; |
|||
} |
|||
|
|||
div.failed { |
|||
color: #fc1; |
|||
font-size: 0.9em; |
|||
} |
|||
|
|||
div.footnote { |
|||
font-size: 0.9em; |
|||
color: #555; |
|||
margin-top: 2em; |
|||
} |
|||
div.footnote:empty { |
|||
display: none; |
|||
} |
|||
|
|||
div.cateogry { |
|||
font-size: 0.75em; |
|||
opacity: 0.75; |
|||
margin-bottom: 1em; |
|||
} |
|||
|
|||
div.result { |
|||
border: 1px solid #000; |
|||
border-radius: 0.5em; |
|||
background-color: #333; |
|||
padding: 0.5em 0; |
|||
color: #eee; |
|||
text-align: center; |
|||
font-size: 1.5em; |
|||
min-height: 1.5em; |
|||
} |
|||
div.result.long { |
|||
font-size: 1.25em; |
|||
padding: 0.75em 0; |
|||
|
|||
} |
|||
|
|||
button { |
|||
background: #333; |
|||
outline: none; |
|||
border: 1px solid #000; |
|||
color: #ccc; |
|||
padding: 0.25em 1ch; |
|||
margin: 1.5em 0 0.5em 0; |
|||
font-size: 1.5em; |
|||
} |
|||
button:hover { |
|||
color: #fff; |
|||
} |
|||
button:active { |
|||
background: #444; |
|||
} |
|||
</style> |
@ -0,0 +1,40 @@ |
|||
<script> |
|||
export let status; |
|||
export let error; |
|||
|
|||
const dev = process.env.NODE_ENV === 'development'; |
|||
</script> |
|||
|
|||
<style> |
|||
h1, p { |
|||
margin: 0 auto; |
|||
} |
|||
|
|||
h1 { |
|||
font-size: 2.8em; |
|||
font-weight: 700; |
|||
margin: 0 0 0.5em 0; |
|||
} |
|||
|
|||
p { |
|||
margin: 1em auto; |
|||
} |
|||
|
|||
@media (min-width: 480px) { |
|||
h1 { |
|||
font-size: 4em; |
|||
} |
|||
} |
|||
</style> |
|||
|
|||
<svelte:head> |
|||
<title>{status}</title> |
|||
</svelte:head> |
|||
|
|||
<h1>{status}</h1> |
|||
|
|||
<p>{error.message}</p> |
|||
|
|||
{#if dev && error.stack} |
|||
<pre>{error.stack}</pre> |
|||
{/if} |
@ -0,0 +1,20 @@ |
|||
import fs from "fs"; |
|||
|
|||
export let collection = JSON.parse(fs.readFileSync("./compiler/output/_collection.json")); |
|||
|
|||
const names = []; |
|||
|
|||
for (const item of collection.items) { |
|||
names.push(JSON.parse(fs.readFileSync(`./compiler/output/${item.name}.json`))); |
|||
} |
|||
|
|||
export function reload() { |
|||
Object.assign(collection, JSON.parse(fs.readFileSync("./compiler/output/_collection.json"))); |
|||
|
|||
names.splice(0); |
|||
for (const item of collection.items) { |
|||
names.push(JSON.parse(fs.readFileSync(`./compiler/output/${item.name}.json`))); |
|||
} |
|||
} |
|||
|
|||
export default names; |
@ -0,0 +1,16 @@ |
|||
import {collection, reload} from './_names.js'; |
|||
|
|||
let lastReload = Date.now(); |
|||
|
|||
export function get(req, res) { |
|||
if ((Date.now() - lastReload) > 10000) { |
|||
reload(); |
|||
lastReload = Date.now(); |
|||
} |
|||
|
|||
res.writeHead(200, { |
|||
'Content-Type': 'application/json' |
|||
}); |
|||
|
|||
res.end(JSON.stringify(collection)); |
|||
} |
@ -0,0 +1,46 @@ |
|||
<script context="module"> |
|||
export function preload() { |
|||
return this.fetch(`index.json`).then(r => r.json()).then(collection => { |
|||
return { collection }; |
|||
}); |
|||
} |
|||
</script> |
|||
|
|||
<script> |
|||
import Page from "../components/Page.svelte"; |
|||
|
|||
|
|||
export let collection; |
|||
</script> |
|||
|
|||
<style> |
|||
ul { |
|||
margin: 0 0 1em 0; |
|||
line-height: 1.5; |
|||
} |
|||
</style> |
|||
|
|||
<svelte:head> |
|||
<title>Blog</title> |
|||
</svelte:head> |
|||
|
|||
<Page narrow selected="INDEX" collection={collection}> |
|||
<h1>Name Generator</h1> |
|||
<p> |
|||
This name generator uses context-free grammars and markov chains to create names faithful to the source material, and does it |
|||
very fast. The code is <a href="https://github.com/gissleh/namegen"> open source</a> and available as a Rust crate. It's |
|||
running as a webassembly module in the browser. |
|||
</p> |
|||
<p> |
|||
The samples used to build the generator has been sourced from both canon and fan-created works. I will not share the sample |
|||
material as I do not possess permissions for that. Feel free to download the processed generator data, however, which you |
|||
can find by tacking .json to any name, and <a href="/index.json">the index</a> if you want a list. The <code>data</code> |
|||
field serializes into a <code>Name</code> struct from the rust crate linked above. |
|||
</p> |
|||
<p> |
|||
There are no analytics or cookies, and all generating happes in your browser. |
|||
</p> |
|||
<p> |
|||
<b>To get started,</b> select a generator from the menu. It's below this text on mobile devices. |
|||
</p> |
|||
</Page> |
@ -0,0 +1,5 @@ |
|||
/target |
|||
**/*.rs.bk |
|||
Cargo.lock |
|||
/node_modules |
|||
/.idea |
@ -0,0 +1,29 @@ |
|||
[package] |
|||
name = "namegen-wasm" |
|||
version = "0.1.0" |
|||
authors = ["Gisle Aune <dev@gisle.me>"] |
|||
edition = "2018" |
|||
|
|||
[package.metadata.wasm-pack.profile.release] |
|||
wasm-opt = ["-Os", "--enable-mutable-globals"] |
|||
lto = true |
|||
opt-level = 's' |
|||
|
|||
[profile.release] |
|||
wasm-opt = ["-Os", "--enable-mutable-globals"] |
|||
lto = true |
|||
opt-level = 's' |
|||
|
|||
[lib] |
|||
crate-type = ["cdylib"] |
|||
|
|||
[features] |
|||
default = ["wee_alloc"] |
|||
|
|||
[dependencies] |
|||
serde = {version = "1.0.104", features = ["derive"]} |
|||
serde_json = "1.0.44" |
|||
wasm-bindgen = {version = "0.2.67", features = ["serde-serialize"]} |
|||
namegen = {version = "0.1.3", features = ["serde"]} |
|||
console_error_panic_hook = "0.1.6" |
|||
wee_alloc = {version = "0.4.5", optional = true} |
@ -0,0 +1 @@ |
|||
default-target = "wasm32-unknown-unknown" |
@ -0,0 +1,196 @@ |
|||
export function load(): Promise<void> |
|||
|
|||
export default class NameGenerator { |
|||
/** |
|||
* Create or load a name generator. |
|||
* @param data Pass data to load the generator from it. NO VALIDATION is applied, so this should only come from |
|||
* a trusted source. |
|||
*/ |
|||
constructor(data?: NameData) |
|||
|
|||
/** |
|||
* This gets the data that can be passed in to recreate this exact generator. It will |
|||
* cross the WASM boundary to grab this data, so use this sparingly. |
|||
*/ |
|||
get data(): NameData |
|||
|
|||
/** |
|||
* Generate one name. |
|||
* |
|||
* @param formatName The format name to generate |
|||
* @param seed Optionally, a seed for the generator. |
|||
* @returns A string if the format exists, null otherwise. |
|||
*/ |
|||
generate(formatName: string, seed?: BigInt): string | null |
|||
|
|||
/** |
|||
* Generate many names. If you need more than a few names, this method will |
|||
* get them for you without crossing the WASM border more than once. |
|||
* |
|||
* Use case: For a website, generating a dozen of them with this and popping |
|||
* them off an array on subsequent generator presses. |
|||
* |
|||
* @param formatName The format name to generate. |
|||
* @param amount Amount of names to generate. |
|||
* @param seed Optionally, a seed for the generator. |
|||
* @returns An array of strings if it works, null otherwise. |
|||
*/ |
|||
generateMany(formatName: string, amount: number, seed?: BigInt): string[] | null |
|||
|
|||
/** |
|||
* Add a new name part. If your IDE is worth its salt, it will hide any options you don't need to |
|||
* care about for the specific name. |
|||
* |
|||
* @param options |
|||
*/ |
|||
addPart(options: AddPartOptions): void |
|||
|
|||
/** |
|||
* Add a new formatting rule. |
|||
* |
|||
* Examples: |
|||
* * `{first_name} {last_name}`: The referred name parts with a space between. |
|||
* * `{first}'{clan} {=vas|=nar} {ship}`: The third `{...}` is either one of these two words. |
|||
* * `{:full_name|:first_name}, the {title}`: The first `{...}` chooses between these two PREVIOUS formats. |
|||
* |
|||
* @param formatName |
|||
* @param formatStr |
|||
*/ |
|||
addFormat(formatName: string, formatStr: string): void |
|||
|
|||
/** |
|||
* Add a sample set to the generator. Different generators want different samples. |
|||
* |
|||
* * `markov`: SampleSet can only include Word samples. WordWeighted is allowed, but the weight is ignored. Labels are ignored. |
|||
* * `cfgrammar`: SampleSet must only include Tokens samples, and the amount of tokens needs to match Labels. Use `*` for an anon. set. |
|||
* * `wordlist`: SampleSet can include Word and WordWeighted samples. Labels are ignored. |
|||
* |
|||
* @param partName |
|||
* @param sampleSet |
|||
*/ |
|||
learn(partName: string, sampleSet: SampleSet) |
|||
|
|||
/** |
|||
* Calls the `data` getter. |
|||
*/ |
|||
toJSON(): NameData |
|||
} |
|||
|
|||
type AddPartOptions = |
|||
| ({ kind: "markov", initialTokens: string[], lrs: boolean, lrm: boolean, lre: boolean, rlf: boolean} & AddPartOptionsCommon) |
|||
| ({ kind: "cfgrammar", initialSubtokens: string[], ral: boolean, rlf: boolean} & AddPartOptionsCommon) |
|||
| ({ kind: "wordlist" } & AddPartOptionsCommon) |
|||
|
|||
|
|||
interface AddPartOptionsCommon {name: string, formatRules: NamePartFormatRule[]} |
|||
|
|||
/** |
|||
* A SampleSet is a versatile structure for feeding name generators learning data. The sample set should |
|||
* be homogenous. |
|||
*/ |
|||
interface SampleSet { |
|||
labels: string[] |
|||
samples: Sample[] |
|||
} |
|||
|
|||
type Sample = |
|||
| { word: string } |
|||
| { wordWeighted: [string, number] } |
|||
| { tokens: string[] } |
|||
|
|||
interface NameData { |
|||
parts: NamePartData[] |
|||
formats: NameFormatData[] |
|||
} |
|||
|
|||
interface NameFormatData { |
|||
name: string |
|||
parts: NameFormatPartData[] |
|||
} |
|||
|
|||
type NameFormatPartData = { part: number } | { format: number } | { text: string } | { random: NameFormatPartData[] } |
|||
|
|||
interface NamePartData { |
|||
name: string |
|||
generator: NamePartGeneratorEnum |
|||
formatRules: NamePartFormatRule[] |
|||
} |
|||
|
|||
type NamePartGeneratorEnum = |
|||
| {markov: MarkovData} |
|||
| {cfgrammar: CFGrammarData} |
|||
| {wordlist: WordListData} |
|||
|
|||
/** |
|||
* The rust enum is showing here. |
|||
*/ |
|||
type NamePartFormatRule = |
|||
| "capitalizeFirst" |
|||
| "capitalizeDefault" |
|||
| {capitalizeAfter: string} |
|||
| {removeChar: string} |
|||
| {replaceChar: {from: string, to: string}} |
|||
|
|||
interface MarkovData { |
|||
tokens: string[] |
|||
maxTokens: number[] |
|||
starts: MarkovStartData[] |
|||
totalStarts: number[] |
|||
nodes: MarkovNodeData[] |
|||
lengths: number[] |
|||
totalLengths: number |
|||
lrs: boolean |
|||
lrm: boolean |
|||
lre: boolean |
|||
rtf: boolean |
|||
} |
|||
|
|||
interface MarkovStartData { |
|||
/** |
|||
* The tokens to start with. |
|||
*/ |
|||
t: [number, number] |
|||
/** |
|||
* Weight, based on frequency in sample data. |
|||
*/ |
|||
w: number |
|||
/** |
|||
* The length if restrictions apply. |
|||
*/ |
|||
l?: number |
|||
/** |
|||
* The children (indices in nodes array) |
|||
*/ |
|||
c: number[] |
|||
} |
|||
|
|||
interface MarkovNodeData { |
|||
/** |
|||
* Parents (sibling indices) |
|||
*/ |
|||
p: number[] |
|||
/** |
|||
* Token index |
|||
*/ |
|||
t: number |
|||
/** |
|||
* Node's weight |
|||
*/ |
|||
w: number |
|||
/** |
|||
* The length of the sample set (if restrictions apply). |
|||
*/ |
|||
l?: number |
|||
/** |
|||
* Children (sibling indices, omitted if empty) |
|||
*/ |
|||
c?: number[] |
|||
} |
|||
|
|||
interface CFGrammarData { |
|||
|
|||
} |
|||
|
|||
interface WordListData { |
|||
|
|||
} |
@ -0,0 +1,64 @@ |
|||
import wasm from "./Cargo.toml"; |
|||
|
|||
let rust = null; |
|||
|
|||
const promise = wasm().then((res) => { |
|||
console.log(res); |
|||
rust = res; |
|||
}).catch(err => { |
|||
console.error("Rust failed to load.") |
|||
console.error("Error:", err); |
|||
return Promise.reject(err); |
|||
}) |
|||
|
|||
export function load() { |
|||
return promise |
|||
} |
|||
|
|||
export default class NameGenerator { |
|||
constructor(data) { |
|||
console.log(rust); |
|||
|
|||
if (data != null) { |
|||
this._gen = rust.WasmNameGenerator.load(JSON.stringify(data)); |
|||
} else { |
|||
this._gen = rust.WasmNameGenerator.new(); |
|||
} |
|||
} |
|||
|
|||
get data() { |
|||
return this._gen.data(); |
|||
} |
|||
|
|||
generate(formatName, seed) { |
|||
return this._gen.generate_one(formatName, seed); |
|||
} |
|||
|
|||
generateMany(formatName, amount, seed) { |
|||
return this._gen.generate_many(formatName, amount, seed); |
|||
} |
|||
|
|||
addPart(options) { |
|||
return this._gen.add_part(options); |
|||
} |
|||
|
|||
addFormat(formatName, formatStr) { |
|||
return this._gen.add_format(formatName, formatStr); |
|||
} |
|||
|
|||
learn(partName, sampleSet) { |
|||
try { |
|||
this._gen.learn(partName, sampleSet) |
|||
} catch(err) { |
|||
throw new Error(err); |
|||
} |
|||
} |
|||
|
|||
toJSON() { |
|||
return this.data; |
|||
} |
|||
|
|||
free() { |
|||
this._gen.free(); |
|||
} |
|||
} |
@ -0,0 +1,338 @@ |
|||
{ |
|||
"requires": true, |
|||
"lockfileVersion": 1, |
|||
"dependencies": { |
|||
"@wasm-tool/rollup-plugin-rust": { |
|||
"version": "1.0.5", |
|||
"resolved": "https://registry.npmjs.org/@wasm-tool/rollup-plugin-rust/-/rollup-plugin-rust-1.0.5.tgz", |
|||
"integrity": "sha512-qVkXNlU3KQmk1zRAIa45yY4WdFA0nlLM8hKv7WGxZptiZA/L+BAscg62U7eqDKjLzCzpUmMXHwN2NHiKrsOvkA==", |
|||
"dev": true, |
|||
"requires": { |
|||
"glob": "^7.1.6", |
|||
"rimraf": "^3.0.0", |
|||
"rollup-pluginutils": "^2.8.2", |
|||
"toml": "^3.0.0", |
|||
"wasm-pack": "^0.9.0" |
|||
} |
|||
}, |
|||
"axios": { |
|||
"version": "0.19.2", |
|||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", |
|||
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", |
|||
"dev": true, |
|||
"requires": { |
|||
"follow-redirects": "1.5.10" |
|||
} |
|||
}, |
|||
"balanced-match": { |
|||
"version": "1.0.0", |
|||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", |
|||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", |
|||
"dev": true |
|||
}, |
|||
"binary-install": { |
|||
"version": "0.0.1", |
|||
"resolved": "https://registry.npmjs.org/binary-install/-/binary-install-0.0.1.tgz", |
|||
"integrity": "sha512-axr6lqB4ec/pkEOb/JMnZpfcroWv1zT48pVz1oQHG7XmGkS77vmdxmP1btuH79lWQdy9e2MVw/uW0D8siopkRg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"axios": "^0.19.0", |
|||
"env-paths": "^2.2.0", |
|||
"mkdirp": "^0.5.1", |
|||
"rimraf": "^3.0.0", |
|||
"tar": "^5.0.5", |
|||
"universal-url": "^2.0.0" |
|||
} |
|||
}, |
|||
"brace-expansion": { |
|||
"version": "1.1.11", |
|||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", |
|||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", |
|||
"dev": true, |
|||
"requires": { |
|||
"balanced-match": "^1.0.0", |
|||
"concat-map": "0.0.1" |
|||
} |
|||
}, |
|||
"chownr": { |
|||
"version": "1.1.4", |
|||
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", |
|||
"integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", |
|||
"dev": true |
|||
}, |
|||
"concat-map": { |
|||
"version": "0.0.1", |
|||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", |
|||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", |
|||
"dev": true |
|||
}, |
|||
"debug": { |
|||
"version": "3.1.0", |
|||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", |
|||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", |
|||
"dev": true, |
|||
"requires": { |
|||
"ms": "2.0.0" |
|||
} |
|||
}, |
|||
"env-paths": { |
|||
"version": "2.2.0", |
|||
"resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.0.tgz", |
|||
"integrity": "sha512-6u0VYSCo/OW6IoD5WCLLy9JUGARbamfSavcNXry/eu8aHVFei6CD3Sw+VGX5alea1i9pgPHW0mbu6Xj0uBh7gA==", |
|||
"dev": true |
|||
}, |
|||
"estree-walker": { |
|||
"version": "0.6.1", |
|||
"resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-0.6.1.tgz", |
|||
"integrity": "sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==", |
|||
"dev": true |
|||
}, |
|||
"follow-redirects": { |
|||
"version": "1.5.10", |
|||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", |
|||
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", |
|||
"dev": true, |
|||
"requires": { |
|||
"debug": "=3.1.0" |
|||
} |
|||
}, |
|||
"fs-minipass": { |
|||
"version": "2.1.0", |
|||
"resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-2.1.0.tgz", |
|||
"integrity": "sha512-V/JgOLFCS+R6Vcq0slCuaeWEdNC3ouDlJMNIsacH2VtALiu9mV4LPrHc5cDl8k5aw6J8jwgWWpiTo5RYhmIzvg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"minipass": "^3.0.0" |
|||
} |
|||
}, |
|||
"fs.realpath": { |
|||
"version": "1.0.0", |
|||
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", |
|||
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", |
|||
"dev": true |
|||
}, |
|||
"fsevents": { |
|||
"version": "2.3.1", |
|||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.1.tgz", |
|||
"integrity": "sha512-YR47Eg4hChJGAB1O3yEAOkGO+rlzutoICGqGo9EZ4lKWokzZRSyIW1QmTzqjtw8MJdj9srP869CuWw/hyzSiBw==", |
|||
"dev": true, |
|||
"optional": true |
|||
}, |
|||
"glob": { |
|||
"version": "7.1.6", |
|||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", |
|||
"integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", |
|||
"dev": true, |
|||
"requires": { |
|||
"fs.realpath": "^1.0.0", |
|||
"inflight": "^1.0.4", |
|||
"inherits": "2", |
|||
"minimatch": "^3.0.4", |
|||
"once": "^1.3.0", |
|||
"path-is-absolute": "^1.0.0" |
|||
} |
|||
}, |
|||
"hasurl": { |
|||
"version": "1.0.0", |
|||
"resolved": "https://registry.npmjs.org/hasurl/-/hasurl-1.0.0.tgz", |
|||
"integrity": "sha512-43ypUd3DbwyCT01UYpA99AEZxZ4aKtRxWGBHEIbjcOsUghd9YUON0C+JF6isNjaiwC/UF5neaUudy6JS9jZPZQ==", |
|||
"dev": true |
|||
}, |
|||
"inflight": { |
|||
"version": "1.0.6", |
|||
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", |
|||
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", |
|||
"dev": true, |
|||
"requires": { |
|||
"once": "^1.3.0", |
|||
"wrappy": "1" |
|||
} |
|||
}, |
|||
"inherits": { |
|||
"version": "2.0.4", |
|||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", |
|||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", |
|||
"dev": true |
|||
}, |
|||
"lodash.sortby": { |
|||
"version": "4.7.0", |
|||
"resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", |
|||
"integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=", |
|||
"dev": true |
|||
}, |
|||
"minimatch": { |
|||
"version": "3.0.4", |
|||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", |
|||
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", |
|||
"dev": true, |
|||
"requires": { |
|||
"brace-expansion": "^1.1.7" |
|||
} |
|||
}, |
|||
"minimist": { |
|||
"version": "1.2.5", |
|||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", |
|||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", |
|||
"dev": true |
|||
}, |
|||
"minipass": { |
|||
"version": "3.1.3", |
|||
"resolved": "https://registry.npmjs.org/minipass/-/minipass-3.1.3.tgz", |
|||
"integrity": "sha512-Mgd2GdMVzY+x3IJ+oHnVM+KG3lA5c8tnabyJKmHSaG2kAGpudxuOf8ToDkhumF7UzME7DecbQE9uOZhNm7PuJg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"yallist": "^4.0.0" |
|||
} |
|||
}, |
|||
"minizlib": { |
|||
"version": "2.1.2", |
|||
"resolved": "https://registry.npmjs.org/minizlib/-/minizlib-2.1.2.tgz", |
|||
"integrity": "sha512-bAxsR8BVfj60DWXHE3u30oHzfl4G7khkSuPW+qvpd7jFRHm7dLxOjUk1EHACJ/hxLY8phGJ0YhYHZo7jil7Qdg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"minipass": "^3.0.0", |
|||
"yallist": "^4.0.0" |
|||
} |
|||
}, |
|||
"mkdirp": { |
|||
"version": "0.5.5", |
|||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", |
|||
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", |
|||
"dev": true, |
|||
"requires": { |
|||
"minimist": "^1.2.5" |
|||
} |
|||
}, |
|||
"ms": { |
|||
"version": "2.0.0", |
|||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", |
|||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", |
|||
"dev": true |
|||
}, |
|||
"once": { |
|||
"version": "1.4.0", |
|||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", |
|||
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", |
|||
"dev": true, |
|||
"requires": { |
|||
"wrappy": "1" |
|||
} |
|||
}, |
|||
"path-is-absolute": { |
|||
"version": "1.0.1", |
|||
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", |
|||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", |
|||
"dev": true |
|||
}, |
|||
"punycode": { |
|||
"version": "2.1.1", |
|||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", |
|||
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", |
|||
"dev": true |
|||
}, |
|||
"rimraf": { |
|||
"version": "3.0.2", |
|||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", |
|||
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", |
|||
"dev": true, |
|||
"requires": { |
|||
"glob": "^7.1.3" |
|||
} |
|||
}, |
|||
"rollup": { |
|||
"version": "2.38.4", |
|||
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.38.4.tgz", |
|||
"integrity": "sha512-B0LcJhjiwKkTl79aGVF/u5KdzsH8IylVfV56Ut6c9ouWLJcUK17T83aZBetNYSnZtXf2OHD4+2PbmRW+Fp5ulg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"fsevents": "~2.3.1" |
|||
} |
|||
}, |
|||
"rollup-pluginutils": { |
|||
"version": "2.8.2", |
|||
"resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz", |
|||
"integrity": "sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==", |
|||
"dev": true, |
|||
"requires": { |
|||
"estree-walker": "^0.6.1" |
|||
} |
|||
}, |
|||
"tar": { |
|||
"version": "5.0.5", |
|||
"resolved": "https://registry.npmjs.org/tar/-/tar-5.0.5.tgz", |
|||
"integrity": "sha512-MNIgJddrV2TkuwChwcSNds/5E9VijOiw7kAc1y5hTNJoLDSuIyid2QtLYiCYNnICebpuvjhPQZsXwUL0O3l7OQ==", |
|||
"dev": true, |
|||
"requires": { |
|||
"chownr": "^1.1.3", |
|||
"fs-minipass": "^2.0.0", |
|||
"minipass": "^3.0.0", |
|||
"minizlib": "^2.1.0", |
|||
"mkdirp": "^0.5.0", |
|||
"yallist": "^4.0.0" |
|||
} |
|||
}, |
|||
"toml": { |
|||
"version": "3.0.0", |
|||
"resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", |
|||
"integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==", |
|||
"dev": true |
|||
}, |
|||
"tr46": { |
|||
"version": "1.0.1", |
|||
"resolved": "https://registry.npmjs.org/tr46/-/tr46-1.0.1.tgz", |
|||
"integrity": "sha1-qLE/1r/SSJUZZ0zN5VujaTtwbQk=", |
|||
"dev": true, |
|||
"requires": { |
|||
"punycode": "^2.1.0" |
|||
} |
|||
}, |
|||
"universal-url": { |
|||
"version": "2.0.0", |
|||
"resolved": "https://registry.npmjs.org/universal-url/-/universal-url-2.0.0.tgz", |
|||
"integrity": "sha512-3DLtXdm/G1LQMCnPj+Aw7uDoleQttNHp2g5FnNQKR6cP6taNWS1b/Ehjjx4PVyvejKi3TJyu8iBraKM4q3JQPg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"hasurl": "^1.0.0", |
|||
"whatwg-url": "^7.0.0" |
|||
} |
|||
}, |
|||
"wasm-pack": { |
|||
"version": "0.9.1", |
|||
"resolved": "https://registry.npmjs.org/wasm-pack/-/wasm-pack-0.9.1.tgz", |
|||
"integrity": "sha512-866+5UIdASabHMVU+M1azbn8tN1g6kLDoL5qvzVYep2hCYicKCgD/Y1LD0yOB3xMDdi+OD51WYNNBGH1NNF23g==", |
|||
"dev": true, |
|||
"requires": { |
|||
"binary-install": "0.0.1" |
|||
} |
|||
}, |
|||
"webidl-conversions": { |
|||
"version": "4.0.2", |
|||
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-4.0.2.tgz", |
|||
"integrity": "sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg==", |
|||
"dev": true |
|||
}, |
|||
"whatwg-url": { |
|||
"version": "7.1.0", |
|||
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-7.1.0.tgz", |
|||
"integrity": "sha512-WUu7Rg1DroM7oQvGWfOiAK21n74Gg+T4elXEQYkOhtyLeWiJFoOGLXPKI/9gzIie9CtwVLm8wtw6YJdKyxSjeg==", |
|||
"dev": true, |
|||
"requires": { |
|||
"lodash.sortby": "^4.7.0", |
|||
"tr46": "^1.0.1", |
|||
"webidl-conversions": "^4.0.2" |
|||
} |
|||
}, |
|||
"wrappy": { |
|||
"version": "1.0.2", |
|||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", |
|||
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", |
|||
"dev": true |
|||
}, |
|||
"yallist": { |
|||
"version": "4.0.0", |
|||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", |
|||
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", |
|||
"dev": true |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,126 @@ |
|||
#[macro_use]
|
|||
extern crate serde;
|
|||
|
|||
use namegen::{Name, SampleSet, NamePart, FormattingRule};
|
|||
use std::error::Error;
|
|||
use wasm_bindgen::prelude::*;
|
|||
|
|||
#[cfg(feature = "wee_alloc")]
|
|||
#[global_allocator]
|
|||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|||
|
|||
|
|||
#[wasm_bindgen]
|
|||
pub struct WasmNameGenerator {
|
|||
name: Name,
|
|||
}
|
|||
|
|||
#[derive(Deserialize)]
|
|||
#[serde(rename_all = "camelCase")]
|
|||
pub struct AddPartOptions {
|
|||
name: String,
|
|||
kind: String,
|
|||
format_rules: Vec<FormattingRule>,
|
|||
#[serde(default)]
|
|||
initial_tokens: Vec<String>,
|
|||
#[serde(default)]
|
|||
initial_subtokens: Vec<String>,
|
|||
#[serde(default)]
|
|||
rlf: bool,
|
|||
#[serde(default)]
|
|||
ral: bool,
|
|||
#[serde(default)]
|
|||
lrs: bool,
|
|||
#[serde(default)]
|
|||
lrm: bool,
|
|||
#[serde(default)]
|
|||
lre: bool,
|
|||
}
|
|||
|
|||
#[wasm_bindgen]
|
|||
impl WasmNameGenerator {
|
|||
pub fn generate_one(&self, format_name: &str, seed: Option<u64>) -> Option<String> {
|
|||
match seed {
|
|||
Some(seed) => self.name.generate_seeded(seed, format_name)?.next(),
|
|||
None => self.name.generate(format_name)?.next(),
|
|||
}
|
|||
}
|
|||
|
|||
pub fn generate_many(&self, format_name: &str, amount: usize, seed: Option<u64>) -> JsValue {
|
|||
let gen = match seed {
|
|||
Some(seed) => self.name.generate_seeded(seed, format_name),
|
|||
None => self.name.generate(format_name),
|
|||
};
|
|||
|
|||
match gen {
|
|||
Some(gen) => {
|
|||
let strings: Vec<String> = gen.take(amount).collect();
|
|||
JsValue::from_serde(&strings).unwrap()
|
|||
}
|
|||
None => JsValue::null(),
|
|||
}
|
|||
}
|
|||
|
|||
pub fn add_part(&mut self, options: JsValue) -> Result<(), JsValue> {
|
|||
let mut options: AddPartOptions = match options.into_serde() {
|
|||
Ok(options) => options,
|
|||
Err(err) => {
|
|||
return Err(JsValue::from(format!("{}", err)));
|
|||
}
|
|||
};
|
|||
|
|||
match options.kind.as_str() {
|
|||
"markov" => {
|
|||
self.name.add_part(NamePart::new_markov(
|
|||
&options.name, &options.format_rules,
|
|||
options.initial_tokens.as_slice(),
|
|||
options.lrs, options.lrm, options.lre, options.rlf,
|
|||
));
|
|||
},
|
|||
"cfgrammar" => {
|
|||
self.name.add_part(NamePart::new_cfgrammar(
|
|||
&options.name, &options.format_rules,
|
|||
options.initial_subtokens.as_slice(),
|
|||
options.rlf, options.ral,
|
|||
));
|
|||
},
|
|||
"wordlist" => {
|
|||
self.name.add_part(NamePart::new_wordlist(&options.name, &options.format_rules));
|
|||
},
|
|||
_ => {
|
|||
return Err(JsValue::from("AddPartError: Invalid value for options.kind (outdated wasm?)"));
|
|||
}
|
|||
}
|
|||
|
|||
Ok(())
|
|||
}
|
|||
|
|||
pub fn add_format(&mut self, format_name: &str, format_str: &str) {
|
|||
self.name.add_format(format_name, format_str)
|
|||
}
|
|||
|
|||
pub fn learn(&mut self, part_name: &str, sample_set: JsValue) -> Result<(), JsValue> {
|
|||
console_error_panic_hook::set_once();
|
|||
let sample_set: SampleSet = match sample_set.into_serde() {
|
|||
Ok(ss) => ss,
|
|||
Err(e) => { return Err(JsValue::from(format!("{}", e))); },
|
|||
};
|
|||
|
|||
self.name.learn(part_name, &sample_set).map_err(|e| JsValue::from(format!("{}", e)))
|
|||
}
|
|||
|
|||
pub fn data(&self) -> JsValue {
|
|||
JsValue::from_serde(&self.name).unwrap()
|
|||
}
|
|||
|
|||
pub fn new() -> WasmNameGenerator {
|
|||
WasmNameGenerator { name: Name::new() }
|
|||
}
|
|||
|
|||
pub fn load(data: &str) -> Result<WasmNameGenerator, JsValue> {
|
|||
let name = serde_json::from_slice(data.as_bytes());
|
|||
return name
|
|||
.map(|n| WasmNameGenerator { name: n })
|
|||
.map_err(|e| JsValue::from(format!("{:?}", e)));
|
|||
}
|
|||
}
|
@ -0,0 +1,17 @@ |
|||
import sirv from 'sirv'; |
|||
import polka from 'polka'; |
|||
import compression from 'compression'; |
|||
import * as sapper from '@sapper/server'; |
|||
|
|||
const { PORT, NODE_ENV } = process.env; |
|||
const dev = NODE_ENV === 'development'; |
|||
|
|||
polka() // You can also use Express
|
|||
.use( |
|||
compression({ threshold: 0 }), |
|||
sirv('static', { dev }), |
|||
sapper.middleware() |
|||
) |
|||
.listen(PORT, err => { |
|||
if (err) console.log('error', err); |
|||
}); |
@ -0,0 +1,86 @@ |
|||
import { timestamp, files, shell } from '@sapper/service-worker'; |
|||
|
|||
const ASSETS = `cache${timestamp}`; |
|||
|
|||
// `shell` is an array of all the files generated by the bundler,
|
|||
// `files` is an array of everything in the `static` directory
|
|||
const to_cache = shell.concat(files); |
|||
const staticAssets = new Set(to_cache); |
|||
|
|||
self.addEventListener('install', event => { |
|||
event.waitUntil( |
|||
caches |
|||
.open(ASSETS) |
|||
.then(cache => cache.addAll(to_cache)) |
|||
.then(() => { |
|||
self.skipWaiting(); |
|||
}) |
|||
); |
|||
}); |
|||
|
|||
self.addEventListener('activate', event => { |
|||
event.waitUntil( |
|||
caches.keys().then(async keys => { |
|||
// delete old caches
|
|||
for (const key of keys) { |
|||
if (key !== ASSETS) await caches.delete(key); |
|||
} |
|||
|
|||
self.clients.claim(); |
|||
}) |
|||
); |
|||
}); |
|||
|
|||
|
|||
/** |
|||
* Fetch the asset from the network and store it in the cache. |
|||
* Fall back to the cache if the user is offline. |
|||
*/ |
|||
async function fetchAndCache(request) { |
|||
const cache = await caches.open(`offline${timestamp}`) |
|||
|
|||
try { |
|||
const response = await fetch(request); |
|||
cache.put(request, response.clone()); |
|||
return response; |
|||
} catch (err) { |
|||
const response = await cache.match(request); |
|||
if (response) return response; |
|||
|
|||
throw err; |
|||
} |
|||
} |
|||
|
|||
self.addEventListener('fetch', event => { |
|||
if (event.request.method !== 'GET' || event.request.headers.has('range')) return; |
|||
|
|||
const url = new URL(event.request.url); |
|||
|
|||
// don't try to handle e.g. data: URIs
|
|||
const isHttp = url.protocol.startsWith('http'); |
|||
const isDevServerRequest = url.hostname === self.location.hostname && url.port !== self.location.port; |
|||
const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname); |
|||
const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset; |
|||
|
|||
if (isHttp && !isDevServerRequest && !skipBecauseUncached) { |
|||
event.respondWith( |
|||
(async () => { |
|||
// always serve static files and bundler-generated assets from cache.
|
|||
// if your application has other URLs with data that will never change,
|
|||
// set this variable to true for them and they will only be fetched once.
|
|||
const cachedAsset = isStaticAsset && await caches.match(event.request); |
|||
|
|||
// for pages, you might want to serve a shell `service-worker-index.html` file,
|
|||
// which Sapper has generated for you. It's not right for every
|
|||
// app, but if it's right for yours then uncomment this section
|
|||
/* |
|||
if (!cachedAsset && url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) { |
|||
return caches.match('/service-worker-index.html'); |
|||
} |
|||
*/ |
|||
|
|||
return cachedAsset || fetchAndCache(event.request); |
|||
})() |
|||
); |
|||
} |
|||
}); |
@ -0,0 +1,33 @@ |
|||
<!doctype html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<meta name="viewport" content="width=device-width,initial-scale=1.0"> |
|||
<meta name="theme-color" content="#333333"> |
|||
|
|||
%sapper.base% |
|||
|
|||
<link rel="stylesheet" href="global.css"> |
|||
<link rel="manifest" href="manifest.json" crossorigin="use-credentials"> |
|||
<link rel="icon" type="image/png" href="favicon.png"> |
|||
|
|||
<!-- Sapper creates a <script> tag containing `src/client.js` |
|||
and anything else it needs to hydrate the app and |
|||
initialise the router --> |
|||
%sapper.scripts% |
|||
|
|||
<!-- Sapper generates a <style> tag containing critical CSS |
|||
for the current page. CSS for the rest of the app is |
|||
lazily loaded when it precaches secondary pages --> |
|||
%sapper.styles% |
|||
|
|||
<!-- This contains the contents of the <svelte:head> component, if |
|||
the current page has one --> |
|||
%sapper.head% |
|||
</head> |
|||
<body> |
|||
<!-- The application will be rendered inside this element, |
|||
because `src/client.js` references it --> |
|||
<div id="sapper">%sapper.html%</div> |
|||
</body> |
|||
</html> |
After Width: 128 | Height: 128 | Size: 3.1 KiB |
@ -0,0 +1,28 @@ |
|||
body { |
|||
margin: 0; |
|||
font-family: Roboto, -apple-system, BlinkMacSystemFont, Segoe UI, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; |
|||
font-size: 14px; |
|||
line-height: 1.5; |
|||
color: #aaa; |
|||
background: #111; |
|||
} |
|||
|
|||
h1, h2, h3, h4, h5, h6 { |
|||
margin: 0 0 0.5em 0; |
|||
font-weight: 400; |
|||
line-height: 1.2; |
|||
} |
|||
|
|||
h1 { |
|||
font-size: 2em; |
|||
} |
|||
|
|||
a { |
|||
color: inherit; |
|||
} |
|||
|
|||
@media (min-width: 400px) { |
|||
body { |
|||
font-size: 16px; |
|||
} |
|||
} |
After Width: 192 | Height: 192 | Size: 4.6 KiB |
After Width: 512 | Height: 512 | Size: 14 KiB |
@ -0,0 +1,20 @@ |
|||
{ |
|||
"background_color": "#ffffff", |
|||
"theme_color": "#333333", |
|||
"name": "TODO", |
|||
"short_name": "TODO", |
|||
"display": "minimal-ui", |
|||
"start_url": "/", |
|||
"icons": [ |
|||
{ |
|||
"src": "logo-192.png", |
|||
"sizes": "192x192", |
|||
"type": "image/png" |
|||
}, |
|||
{ |
|||
"src": "logo-512.png", |
|||
"sizes": "512x512", |
|||
"type": "image/png" |
|||
} |
|||
] |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue