Greasy Fork is available in English.

§
Posté le: 12/03/2015
Édité le: 12/03/2015

Text correction

Below is a generated txt that a site I am on has when importing a movie. Kinda messed up as you can see. I was hoping to have a GM script to parse the page and fix these errors.

What I need it to look like is this:

[nm=nm0000237]John Travolta[/nm] - Bolt (voice)
[nm=nm1415323]Miley Cyrus[/nm] - Penny (voice)


What I get is this and I have to go in and change things by hand.

[nm=0000237]John.Travolta[/nm].-.Bolt.
..
..
..(voice)
[nm=1415323]Miley.Cyrus[/nm].-.Penny.
..
..
..(voice)

I placed periods to make it obvious where the spaces are.

The [nm=nm is the important thing and I was hoping for the other as well. This is all in a text area.

woxxomMod
§
Posté le: 12/03/2015
Édité le: 12/03/2015

Well, the replacement part is simple:

sourceText.replace(/(\[nm=)(.+?\])\s*-\s*(.+?)[\r\n\s]+(\(.+?\))/g, '$1nm$2 - $3 $4')

This is as much as possible without further specifics.

§
Posté le: 12/03/2015

Sorry. Keep forgetting you need alot of the source to help out.

http://pastebin.com/9p9ysngD

woxxomMod
§
Posté le: 12/03/2015

Something like this then:

// ==UserScript==
// @name        Reformat
// @include     http://*************
// @grant       none
// @run-at      document-end
// ==/UserScript==

var textarea = document.querySelector('textarea#descr');
textarea.value = textarea.value.replace(/(\[nm=)(.+?\])\s*-\s*(.+?)[\r\n\s]+(?:([^(]+?)?[\r\n\s]*)(\(.+?\))/g, '$1nm$2 - $3$4 $5');

If the textarea is populated dynamically with js, this won't work, a MutationObserver will be needed.

§
Posté le: 12/03/2015
Édité le: 12/03/2015

OK. Worked great as is. Only issue is that it does not work if not using (Voice) in the lines. IE if I do a regular movie import without voice actors it completely fails. I will actually be doing more without the (voice) so it can be left out entirely or a workaround if you know of one.

-edit

In playing with the script it seems to work great in specific instances. Was wondering if it could be split into separate parts. IE it deals with the nm=nm issue and then deals with the spacing on another line. Tried this on multiple movies and if there was 20 lines three would be fine but the rest would be as if the script did not run.

Again thanks for the help and the most important thing is the nm=nm over all.

woxxomMod
§
Posté le: 12/03/2015

Splitting is easy:

textarea.value = textarea.value
    .replace(/(\[nm=)(.+?\])/g, '$1nm$2')
    .replace(/(\[nm=.+?\])\s*-\s*(.+?)[\r\n\s]+/g, '$1 - $2')
    .replace(/(\[nm=.+?\]\s*-\s*.+?)[\r\n\s]+(\(.+?\))/g, '$1 $2')

Untested though.

§
Posté le: 12/03/2015

Nope, did not work at all.

woxxomMod
§
Posté le: 12/03/2015

Well, it was an abstract example of splitting, not the ready-to-use code.

§
Posté le: 12/03/2015

Gotcha. Will play with this and get back.

§
Posté le: 12/03/2015
Édité le: 12/03/2015

Thanks for the assist. Now that I know how to replace bad text with JS, editing pages have gotten easier. I now have things in working order.

Now to try and teach myself RegEx to not be so greedy.

woxxomMod
§
Posté le: 12/03/2015
Édité le: 12/03/2015

There should be some user-friendly tutorials that won't require you spending lots of time on learning. Tried googling, found something - move the mouse over /([A-Z])\w+/ and it'll show helpful tooltips, you can edit the expression and use an actual text instead of the demo, there are some examples in the menu on the left. You might find a better site, though, if you try.

Poster une réponse

Connectez-vous pour poster une réponse.