我有一个上传的rtf文件,一旦上传,我希望它被解析并转换为json文件。我使用的主要是角度。我上传工作正常,我只是解决问题。该文件确实被清理,但它没有正确格式化为json文件。任何人都有任何关于最好的事情的建议吗?
this.parseFile=function(file, uploadUrl){
reader.onload = function(onLoadEvent){
$rootScope.$apply(function(){
var dataURL = reader.result;
setUpData(dataURL);
});
};
reader.readAsText(file);
};
function saveFile(file){
var fd = new FormData();
fd.append('save_file', file);
$http({
method: 'POST',
url: "includes/loadFile.php",
data: fd,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function (data, status, headers, config) {
msg = {
title: "success",
cls: "success",
msg: data
};
popUp(msg);
})
.error(function (data, status, header, config) {
msg = {
title: "error",
cls: "danger",
msg: data
};
popUp(msg);
});
};
function setUpData(data)
{
text = data;
text = text.replace('/[\p{Z}\s]{2,}/u', ' ');
// skip over the heading stuff
j= text.indexOf('{',1); // skip ahead to the first part of the header
var loc = 1;
var t ="";
var ansa="";
len = text.length;
getpgraph(); // skip by the first paragrap
while(j<len) {
c = text.substr(j,1);
if (c=="\\") {
// have a tag
var tag = gettag();
if (tag.length > 0) {
// process known tags
switch (tag) {
case 'par':
ansa+="\r\n";
break;
// ad a list of common tags
// parameter tags
case 'spriority1':
case 'fprq2':
case 'author':
case 'operator':
case 'sqformat':
case 'company':
case 'xmlns1':
case 'wgrffmtfilter':
case 'pnhang':
case 'themedata':
case 'colorschememapping':
var tt = gettag();
break;
case '*':
case 'info':
case 'stylesheet':
// gets to end of paragraph
j--;
getpgraph();
default:
// ignore the tag
}
}
} else {
ansa += c;
}
j++;
};
ansa = ansa.replace(/{|}|"|( | )\r/g,'');
ansa = ansa.replace(/Amount After|Amt After/g,'');
ansa = ansa.replace(/negetive amount means Refund NOTE: Layaways are not in Sales Report till they're paid off/g,'');
ansa = ansa.replace("On Line Vendor Sales Quick Report by Month",'');
ansa = ansa.replace(/ ( ) /g,'');
ansa.trim();
ansa.split("\n");
console.log(ansa);
var newData = new Array();
var vendor = new Array();
var orders = new Array();
var v = 0;
//Lets look for the line that has the word vendor
vendorLine = ansa.match(/Vendor/gi);
//Look for each order for the vendor
orderLine = ansa.match(/date/gi);
figuresLine = ansa.match(/figures/g);
var start;
var end;
for(arr = 0; arr <=ansa.length; arr++)
{
}
if(vendorLine)
{
vendorInfo = ansa.split(' ',ansa.replace('/Vendor|\r|\s/','') );
vendorFName = vendorInfo[0];
vendorLName = vendorInfo[1];
vendorID = vendorInfo[2];
vendor = {vendorid: vendorID, vendorInfo:{fname: vendorFName,lname:vendorLName} , orders:orders};
}
if( orderLine )
{
start = 1;
}
if( figuresLine )
{
end = 0;
}
if( !end || !start)
{
if( end < start )
{
ordersLine = explode(" ",v);
orderDesc = new Array();
//Remove the stuff we dont need. Like Qty ,Subtotal and Inv#. Once we remove them we reindex them
unset(ordersLine[0],ordersLine[2],ordersLine[count(ordersLine)]); // Removes Subtotal and Inv#
unset(ordersLine[count(ordersLine)]); // Removes QTY
ordersLine = array_values(ordersLine);
orderDate = ordersLine[0];
orderAmt = end(ordersLine);
/*foreach (ordersLine as key=>value)
{
if(key != 0)
{
if(key != count(ordersLine)-1)
{
array_push(orderDesc,value);
orderLines = array("vendorid"=>vendorID,"order"=>array("date"=> orderDate,"desc"=> implode(" " ,orderDesc), "amount"=>orderAmt) );
}
};
}*/
}
}
vendor['orders'] = orders;
saveFile(ansa);
}
function gettag() {
// gets the text following the / character or gets the param if it there
var tag='';
while(true) {
j++;
if (j>=len) break;
c = text.substr(j,1);
if (c==';') break;
if (c=='}') break;
if (c=="\\") {
j--;
break;
}
if (c=="{") {
//getpgraph();
break;
}
if (((c>='0')&&(c<='9'))||((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'))||c=="'"||c=="-"||c=="*" ){
tag= tag+c;
} else {
// end of tag
j--;
break;
}
}
return tag;
}
function getpgraph() {
// if the first char after a tag is { then throw out the entire paragraph
// this has to be nested
var nest = 0;
while(true) {
j++;
if (j>=len) break;
if (text.substr(j,1)=='}') {
if (nest==0) return;
nest--;
}
if (text.substr(j,1)=='{') {
nest++;
}
}
return;
}
我对此的期望是创建一个干净的格式良好的JSON文件,然后它可以在它们所在的页面上显示回用户。基本上使用新上传的解析数据刷新页面。
答案 0 :(得分:0)
有一个JS RTF解析器可能有所帮助。然后,您需要对结果进行json编码。 https://github.com/lazygyu/RTF-parser
不确定这是否能解答您的问题,但更多信息可能有助于锁定修复程序。