Skip to content

Instantly share code, notes, and snippets.

@darkash
Last active March 13, 2021 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkash/7de2f875c9251c0e7d8e97631e0a22fc to your computer and use it in GitHub Desktop.
Save darkash/7de2f875c9251c0e7d8e97631e0a22fc to your computer and use it in GitHub Desktop.
converter url post/thread lama ke format POST atau THREAD
// @ts-check
const fs = require('fs');
const path = require('path');
// CATATAN:
// Untuk Path, apabila di CMD tertera sebagai C:\Users\darkash\Desktop
// maka setiap backslash "\" wajib diulang 2x menjadi C:\\Users\\darkash\\Desktop
const inputPath = path.format({
dir: 'C:\\Users\\darkash\\Desktop', // lokasi folder file input, disesuaikan
base: 'input.txt' // file txt yang mau di convert
});
const outputPath = path.format({
dir: 'C:\\Users\\darkash\\Desktop', // lokasi folder output, disesuaikan
base: 'output.txt' // file txt setelah diconvert
});
// handle default url title: [URL]https://forum.indowebster.com/showthread.php?t=xxxx&p=xxxxx[/url]
const lazyRegex = /\[URL\](http|https):\/\/forum\.indowebster\.com\/.+\?(?<thread>t=\d+){0,1}&{0,1}(?<post>p=\d+){0,1}.+\[\/URL\]/i;
// handle custom url title: [url=https://forum.indowebster.com/showthread.php?t=xxxx&p=xxxxx]IDWS[/url]
const diligentRegex = /\[URL=\"{0,1}(http|https):\/\/forum\.indowebster\.com\/.+\?(?<thread>t=\d+){0,1}&{0,1}(?<post>p=\d+){0,1}.*\"{0,1}\](?<title>.*)\[\/URL\]/i;
const postUrlRegex = /\[url\].+\[\/url\]|\[url=.+].*\[\/url\]/i;
const threadUrlRegex = /\[url\].+\[\/url\]|\[url=.+].*\[\/url\]/i;
const file = fs.readFileSync(inputPath);
const text = file.toString();
const splitted = text.split('\n');
let tempOutput = [];
splitted.map((line) => {
let match;
match = line.match(lazyRegex) || line.match(diligentRegex);
if (match === null) {
tempOutput.push(line);
return;
}
const { post, thread, title } = match.groups;
if (typeof post === 'undefined' && typeof thread === 'undefined') {
tempOutput.push(line);
return;
}
if (typeof post !== 'undefined' && post.length > 0) {
const postTitle = title || `[PLAIN]https://forum.idws.id/posts/${post.slice(2, post.length)}[/PLAIN]`;
const replacement = line.replace(postUrlRegex, `[U][POST=${post.slice(2, post.length)}]${postTitle}[/POST][/U]`);
tempOutput.push(replacement);
return;
}
const threadTitle = title || `[PLAIN]https://forum.idws.id/threads/${thread.slice(2, thread.length)}[/PLAIN]`;
const replacement = line.replace(threadUrlRegex, `[U][THREAD=${thread.slice(2, thread.length)}]${threadTitle}[/THREAD][/U]`);
tempOutput.push(replacement);
return;
});
fs.writeFileSync(outputPath, tempOutput.join('\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment