using std::vector;
using std::map;
using std::cout;
+using std::list;
using boost::optional;
using boost::function;
using namespace boost::algorithm;
);
}
+list<RawSubtitle>
+SSAReader::parse_line (RawSubtitle base, string line)
+{
+ enum {
+ TEXT,
+ STYLE,
+ BACKSLASH
+ } state = TEXT;
+
+ list<RawSubtitle> subs;
+ RawSubtitle current = base;
+ string style;
+
+ current.vertical_position.line = 0;
+
+ for (size_t i = 0; i < line.length(); ++i) {
+ char const c = line[i];
+ switch (state) {
+ case TEXT:
+ if (c == '{') {
+ state = STYLE;
+ } else if (c == '\\') {
+ state = BACKSLASH;
+ } else {
+ current.text += c;
+ }
+ break;
+ case STYLE:
+ if (c == '}') {
+ if (!current.text.empty ()) {
+ subs.push_back (current);
+ current.text = "";
+ }
+ if (style == "i1") {
+ current.italic = true;
+ } else if (style == "i0") {
+ current.italic = false;
+ }
+ style = "";
+ state = TEXT;
+ } else {
+ style += c;
+ }
+ break;
+ case BACKSLASH:
+ if (c == 'n' && !current.text.empty ()) {
+ subs.push_back (current);
+ current.text = "";
+ current.vertical_position.line = current.vertical_position.line.get() + 1;
+ }
+ state = TEXT;
+ break;
+ }
+ }
+
+ if (!current.text.empty ()) {
+ subs.push_back (current);
+ }
+
+ return subs;
+}
+
void
SSAReader::read (function<optional<string> ()> get_line)
{
SSAReader (FILE* f);
SSAReader (std::string const & subs);
+ static std::list<RawSubtitle> parse_line (RawSubtitle base, std::string line);
+
private:
void read (boost::function<boost::optional<std::string> ()> get_line);
Time parse_time (std::string t) const;
BOOST_CHECK (i == subs.end());
}
+
+BOOST_AUTO_TEST_CASE (ssa_reader_line_test1)
+{
+ sub::RawSubtitle base;
+ list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "This is a line with some {i1}italics{i0} and then\\nthere is a new line.");
+
+ list<sub::RawSubtitle>::const_iterator i = r.begin ();
+ BOOST_CHECK_EQUAL (i->text, "This is a line with some ");
+ BOOST_CHECK_EQUAL (i->italic, false);
+ ++i;
+ BOOST_REQUIRE (i != r.end ());
+
+ BOOST_CHECK_EQUAL (i->text, "italics");
+ BOOST_CHECK_EQUAL (i->italic, true);
+ ++i;
+ BOOST_REQUIRE (i != r.end ());
+
+ BOOST_CHECK_EQUAL (i->text, " and then");
+ BOOST_CHECK_EQUAL (i->italic, false);
+ ++i;
+ BOOST_REQUIRE (i != r.end ());
+
+ BOOST_CHECK_EQUAL (i->text, "there is a new line.");
+ ++i;
+ BOOST_REQUIRE (i == r.end ());
+}
+
+BOOST_AUTO_TEST_CASE (ssa_reader_line_test2)
+{
+ sub::RawSubtitle base;
+ list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "{i1}It's all just italics{i0}");
+
+ list<sub::RawSubtitle>::const_iterator i = r.begin ();
+ BOOST_CHECK_EQUAL (i->text, "It's all just italics");
+ BOOST_CHECK_EQUAL (i->italic, true);
+ ++i;
+ BOOST_REQUIRE (i == r.end ());
+}