Add the library
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2022-05-12 20:53:26 +02:00
parent cfa0f42103
commit 44d7a9bfdd
Signed by: sam
GPG key ID: 4D8B07C18F31ACBD
8 changed files with 391 additions and 2 deletions

15
.drone.yml Normal file
View file

@ -0,0 +1,15 @@
kind: pipeline
type: docker
name: default
steps:
- name: lint
image: denoland/deno:alpine
commands:
- deno fmt --check
- deno lint
- name: test
image: denoland/deno:alpine
commands:
- deno test

137
.gitignore vendored Normal file
View file

@ -0,0 +1,137 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.dccache
# npm output
npm/

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}

View file

@ -1,6 +1,7 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2022 Sam Thearpy <sam@samtherapy.net>
Copyright (c) 2014 Silvia Pfeiffer <silviapfeiffer1@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View file

@ -1,2 +1,8 @@
# srttovtt
# SRT-to-VTT
## A basic library to convert from srt to vtt
This code is taken from the excellent [webvtt.org](https://www.webvtt.org/), I
merely adapted it to work as a library.
## Usage

94
mod.ts Normal file
View file

@ -0,0 +1,94 @@
// SPDX-License-Identifier: MIT
/**
* @param {string} data SRT text to be converted
* @returns {string} VTT text
*/
export default function srt2webvtt(data: string): string {
// remove dos newlines
let srt = data.replace(/\r+/g, "");
// trim white space start and end
srt = srt.replace(/^\s+|\s+$/g, "");
// get cues
const cuelist = srt.split("\n\n");
let result = "";
if (cuelist.length > 0) {
result += "WEBVTT\n\n";
for (let i = 0; i < cuelist.length; i = i + 1) {
result += convertSrtCue(cuelist[i]);
}
}
return result;
}
/**
*
* @param {string} caption SRT section for parsing
* @returns {string} translated VTT section
*/
function convertSrtCue(caption: string): string {
// remove all html tags for security reasons
//srt = srt.replace(/<[a-zA-Z\/][^>]*>/g, '');
let cue = "";
const s = caption.split(/\n/);
// concatenate muilt-line string separated in array into one
while (s.length > 3) {
for (let i = 3; i < s.length; i++) {
s[2] += "\n" + s[i];
}
s.splice(3, s.length - 3);
}
let line = 0;
// detect identifier
if (!s[0].match(/\d+:\d+:\d+/) && s[1].match(/\d+:\d+:\d+/)) {
cue += s[0].match(/\w+/) + "\n";
line += 1;
}
// get time strings
if (s[line].match(/\d+:\d+:\d+/)) {
// convert time string
const m = s[1].match(
/(\d+):(\d+):(\d+)(?:,(\d+))?\s*--?>\s*(\d+):(\d+):(\d+)(?:,(\d+))?/,
);
if (m) {
cue += m[1] +
":" +
m[2] +
":" +
m[3] +
"." +
m[4] +
" --> " +
m[5] +
":" +
m[6] +
":" +
m[7] +
"." +
m[8] +
"\n";
line += 1;
} else {
// Unrecognized timestring
return "";
}
} else {
// file format error or comment lines
return "";
}
// get cue text
if (s[line]) {
cue += s[line] + "\n\n";
}
return cue;
}

31
scripts/npm-build.ts Normal file
View file

@ -0,0 +1,31 @@
// ex. scripts/build_npm.ts
import { build, emptyDir } from "https://deno.land/x/dnt/mod.ts";
await emptyDir("./npm");
await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
shims: {
// see JS docs for overview and more options
deno: true,
},
package: {
// package.json properties
name: "srt-to-vtt",
version: Deno.args[0],
description: "A library that converts SRT files to VTT equivalents so they can be broadcast on the Internet.",
license: "MIT",
repository: {
type: "git",
url: "git+https://git.froth.zone/sam/srt-to-vtt",
},
bugs: {
url: "https://git.froth.zone/sam/srt-to-vtt/issues",
},
},
});
// post build steps
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");

100
test.ts Normal file
View file

@ -0,0 +1,100 @@
// SPDX-License-Identifier: MIT
import convert from "./mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
Deno.test("convert, 1 sub", () => {
const srt = `1
00:00:00,000 --> 00:00:00,000
Hello, world!
`;
const expected = `WEBVTT
1
00:00:00.000 --> 00:00:00.000
Hello, world!
`;
assertEquals(convert(srt), expected);
});
Deno.test("convert, multiple subs", () => {
const srt = `1
00:02:09,545 --> 00:02:12,757
rare roasted partridge breast
in raspberry coulis
with a sorrel timbale.
2
00:02:13,007 --> 00:02:16,093
...and grilled
free-range rabbit
with herbed french fries.
3
00:02:16,302 --> 00:02:20,014
Our pasta tonight is
a squid ravioli
in a lemon grass broth.
4
00:02:24,602 --> 00:02:28,731
God, I hate this place.
It's a chick's restaurant.
Why aren't we at Dorsia?
5
00:02:28,898 --> 00:02:30,816
Because Bateman won't give
the maitre d' head.
`;
const expected = `WEBVTT
1
00:02:09.545 --> 00:02:12.757
rare roasted partridge breast
in raspberry coulis
with a sorrel timbale.
2
00:02:13.007 --> 00:02:16.093
...and grilled
free-range rabbit
with herbed french fries.
3
00:02:16.302 --> 00:02:20.014
Our pasta tonight is
a squid ravioli
in a lemon grass broth.
4
00:02:24.602 --> 00:02:28.731
God, I hate this place.
It's a chick's restaurant.
Why aren't we at Dorsia?
5
00:02:28.898 --> 00:02:30.816
Because Bateman won't give
the maitre d' head.
`;
assertEquals(convert(srt), expected);
});
Deno.test("Invalid time string", () => {
const srt = `1
00:00:00,000,0 --> 00:00:00,000
Hello, world!
`;
assertEquals(convert(srt), "WEBVTT\n\n");
});
Deno.test("Invalid file", () => {
const srt = `1
// This is a comment
Hello, world!
`;
assertEquals(convert(srt), "WEBVTT\n\n");
});