Last active
December 15, 2015 21:19
-
-
Save dannvix/5325379 to your computer and use it in GitHub Desktop.
very simple parser for 《蟲族之心》subtitles in CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env coffee | |
class SubtitleParser | |
LINE_PATTERN = /// | |
subtitle #line prefix | |
\s+ | |
(\d+) # timestamp | |
\s+ | |
(\d+) # duration | |
\s+ | |
\[\[ | |
(.+) # text | |
\]\] | |
/// | |
@parse: (subtitle) -> | |
lines = subtitle.split("\n") | |
version = parseInt(lines[0].match(/version\s+(\d+)/)[1]) | |
fps = parseFloat(lines[1].match(/fps\s+(\d+\.?\d*)/)[1]) | |
parsedLines = lines[2..].map (line) -> | |
matches = line.match(LINE_PATTERN) | |
lineObject = | |
timestamp: parseInt(matches[1]) | |
duration: parseInt(matches[2]) | |
text: matches[3] | |
ret = | |
version: version | |
fps: fps | |
lines: parsedLines | |
main = -> | |
subtitleData = """ | |
version 1 | |
fps 24.000000 | |
subtitle 33370 2000 [[<c val="FFD700">凱莉根:</c> 我就是蟲群。]] | |
subtitle 36700 2000 [[<c val="FFD700">凱莉根:</c> 敵軍必將敗亡,]] | |
subtitle 40700 2000 [[<c val="FFD700">凱莉根:</c> 星球必成火海。]] | |
subtitle 106080 2000 [[<c val="FFD700">凱莉根:</c> 就是現在,]] | |
subtitle 108700 2000 [[<c val="FFD700">凱莉根:</c> 就在這裡,]] | |
subtitle 111370 2600 [[<c val="FFD700">凱莉根:</c> 我要報仇。]] | |
subtitle 125040 4600 [[<c val="FFD700">凱莉根:</c> 因為我就是刀鋒女皇。]] | |
""" | |
console.log SubtitleParser.parse(subtitleData) | |
# check if we're required as a module | |
if require.main is module | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment