1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <extension/subtitleformat.h>
#include <utility.h>
// format: blank lines separate subtitles or every line is a new subtitle
// depending on the preferences set in:
// plain-text/export-bl-between-subtitles
// plain-text/import-bl-between-subtitles
class PlainTextFormat : public SubtitleFormatIO {
public:
void open(Reader& file) {
Subtitles subtitles = document()->subtitles();
Glib::ustring line;
bool usebl = cfg::get_boolean("plain-text", "import-bl-between-subtitles");
if (!usebl) {
// ignore blank lines
while (file.getline(line)) {
Subtitle sub = subtitles.append();
sub.set_text(line);
}
} else {
// separate subtitles at blank lines
Glib::ustring subtext;
subtext.clear();
int textlines = 0;
while (file.getline(line)) {
if (line.empty()) {
if (textlines > 0) {
Subtitle sub = subtitles.append();
sub.set_text(subtext);
subtext.clear();
textlines = 0;
}
} else {
if (textlines > 0)
subtext += "\n";
subtext += line;
textlines++;
}
}
// if the file didn't end with a blank line, we need to append leftover
// lines as one more subtitle
if (textlines > 0) {
Subtitle sub = subtitles.append();
sub.set_text(subtext);
subtext.clear();
}
} // separate with blank lines
}
void save(Writer& file) {
Document* doc = document();
bool usebl = cfg::get_boolean("plain-text", "export-bl-between-subtitles");
// how many subtitles does this document have?
int subcnt = doc->subtitles().size();
if (subcnt <= 0) // no subtitles, nothing to do
return;
// initialize the output loop
subcnt--; // output all subtitles except the last one.
int i = 0;
Subtitle sub = doc->subtitles().get_first();
while (i < subcnt) {
file.write(sub.get_text() + "\n");
if (usebl)
file.write("\n");
++sub;
i++;
}
// Now, output the last subtitle with no blank line appended.
file.write(sub.get_text() + "\n");
}
};
class PlainTextFormatPlugin : public SubtitleFormat {
public:
SubtitleFormatInfo get_info() {
SubtitleFormatInfo info;
info.name = "Plain Text Format";
info.extension = "txt";
// The Plaint Text Format can import any text file regardless of its
// contents, so the actual pattern would be ".*". But then it would steal
// all subtitle files, such as .srt, .mpsub, etc, from their correct format
// interpreters and digest them all as plain text, which would be wrong. For
// that reason, it must never identify any file as its own and let the more
// picky subtitle formats decide if they want to process a file or not.
info.pattern = "nEvEr MaTcH a PlAiN-texT fILe autOmatIcallY";
return info;
}
SubtitleFormatIO* create() {
PlainTextFormat* sf = new PlainTextFormat();
return sf;
}
};
REGISTER_EXTENSION(PlainTextFormatPlugin)
|