You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.0 KiB
97 lines
2.0 KiB
11 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text.RegularExpressions;
|
||
11 years ago
|
using Fungus;
|
||
11 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
11 years ago
|
/**
|
||
10 years ago
|
* Parses an exported strings file using the Fountain file format for screenplays
|
||
|
* See http://fountain.io for details.
|
||
|
* We only support a small subset of Fountain markup, and use note tags to embed meta data to
|
||
|
* bind dialogue text to the corresponding Say / Menu commands.
|
||
11 years ago
|
*/
|
||
10 years ago
|
public class StringsParser
|
||
11 years ago
|
{
|
||
10 years ago
|
public class StringItem
|
||
11 years ago
|
{
|
||
10 years ago
|
public string[] parameters;
|
||
|
public string bodyText;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
public virtual List<StringItem> ProcessText(string text)
|
||
11 years ago
|
{
|
||
10 years ago
|
List<StringItem> items = new List<StringItem>();
|
||
|
|
||
11 years ago
|
// Split text into lines. Add a newline at end to ensure last command is always parsed
|
||
|
string[] lines = Regex.Split(text + "\n", "(?<=\n)");
|
||
|
|
||
10 years ago
|
int i = 0;
|
||
|
while (i < lines.Length)
|
||
11 years ago
|
{
|
||
10 years ago
|
string line = lines[i].Trim();
|
||
11 years ago
|
|
||
10 years ago
|
if (!(line.StartsWith("[[") && line.EndsWith("]]")))
|
||
11 years ago
|
{
|
||
10 years ago
|
i++;
|
||
11 years ago
|
continue;
|
||
|
}
|
||
|
|
||
10 years ago
|
string blockTag = line.Substring(2, line.Length - 4);
|
||
11 years ago
|
|
||
10 years ago
|
// Find next empty line, #, [[ or eof
|
||
|
int start = i + 1;
|
||
|
int end = lines.Length - 1;
|
||
|
for (int j = start; j <= end; ++j)
|
||
11 years ago
|
{
|
||
10 years ago
|
string line2 = lines[j].Trim();
|
||
|
|
||
|
if (line2.Length == 0 ||
|
||
|
line2.StartsWith("#") ||
|
||
|
line2.StartsWith("[["))
|
||
11 years ago
|
{
|
||
10 years ago
|
end = j;
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
if (end > start)
|
||
11 years ago
|
{
|
||
10 years ago
|
string blockBuffer = "";
|
||
|
for (int j = start; j <= end; ++j)
|
||
11 years ago
|
{
|
||
10 years ago
|
blockBuffer += lines[j].Trim() + "\n";
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
blockBuffer = blockBuffer.Trim();
|
||
|
|
||
|
StringItem item = CreateItem(blockTag, blockBuffer);
|
||
|
if (item != null)
|
||
11 years ago
|
{
|
||
10 years ago
|
items.Add(item);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
i = end + 1;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
return items;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
protected StringItem CreateItem(string commandInfo, string bodyText)
|
||
|
{
|
||
|
string[] parameters = commandInfo.Split(new char[] { ',' });
|
||
|
if (parameters.Length > 0)
|
||
|
{
|
||
|
StringItem item = new StringItem();
|
||
|
item.parameters = parameters;
|
||
|
item.bodyText = bodyText;
|
||
|
return item;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
return null;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|