mirror of
https://github.com/stolksdorf/homebrewery.git
synced 2025-12-20 05:01:58 +00:00
New html validator is working
This commit is contained in:
@@ -13,77 +13,67 @@ renderer.html = function (html) {
|
|||||||
return html;
|
return html;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const tagTypes = ['div', 'span', 'a'];
|
||||||
|
const tagRegex = new RegExp('(' +
|
||||||
|
_.map(tagTypes, (type)=>{
|
||||||
|
return `\\<${type}|\\</${type}>`;
|
||||||
|
}).join('|') + ')', 'g');
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
render : (rawText)=>{
|
marked : Markdown,
|
||||||
return Markdown(rawText, {renderer : renderer})
|
render : (rawBrewText)=>{
|
||||||
|
return Markdown(rawBrewText, {renderer : renderer})
|
||||||
},
|
},
|
||||||
|
|
||||||
validate : (rawText) => {
|
validate : (rawBrewText) => {
|
||||||
|
|
||||||
var res = xmllint.validateXML({
|
|
||||||
xml: rawText,
|
|
||||||
schema: "String"
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(res);
|
|
||||||
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
validate2 : (rawText)=>{
|
|
||||||
var currentLine = 0;
|
|
||||||
var errors = [];
|
var errors = [];
|
||||||
var tokens = Markdown.lexer(rawText, {renderer : renderer});
|
var leftovers = _.reduce(rawBrewText.split('\n'), (acc, line, _lineNumber) => {
|
||||||
|
var lineNumber = _lineNumber + 1;
|
||||||
|
var matches = line.match(tagRegex);
|
||||||
|
if(!matches || !matches.length) return acc;
|
||||||
|
|
||||||
return _.filter(_.map(tokens, (token)=>{
|
_.each(matches, (match)=>{
|
||||||
if(token.type === 'paragraph' || token.type === 'html'){
|
_.each(tagTypes, (type)=>{
|
||||||
|
if(match == `<${type}`){
|
||||||
var hasOpen = token.text.indexOf('<div') !== -1;
|
acc.push({
|
||||||
var hasClose = token.text.indexOf('</div>') !== -1;
|
type : type,
|
||||||
|
line : lineNumber
|
||||||
|
});
|
||||||
if(hasOpen && !hasClose){
|
|
||||||
return {
|
|
||||||
err : 'No closing tag',
|
|
||||||
token : token,
|
|
||||||
line : currentLine
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if(hasClose && !hasOpen){
|
|
||||||
if(token.text.length > 6){
|
|
||||||
return {
|
|
||||||
err : 'Closing tags must be on their own line',
|
|
||||||
token : token,
|
|
||||||
line : currentLine
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
return {
|
if(match === `</${type}>`){
|
||||||
err : 'No opening tag',
|
if(!acc.length){
|
||||||
token : token,
|
errors.push({
|
||||||
line : currentLine
|
line : lineNumber,
|
||||||
};
|
type : type,
|
||||||
}
|
err : 'Unmatched closing tag'
|
||||||
|
});
|
||||||
|
}else if(_.last(acc).type == type){
|
||||||
|
acc.pop();
|
||||||
|
}else{
|
||||||
|
errors.push({
|
||||||
|
line : _.last(acc).line + ' to ' + lineNumber,
|
||||||
|
type : type,
|
||||||
|
err : 'Type mismatch on closing tag'
|
||||||
|
});
|
||||||
|
acc.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
|
||||||
}
|
_.each(leftovers, (unmatched)=>{
|
||||||
//console.log(token);
|
errors.push({
|
||||||
|
line : unmatched.line,
|
||||||
//currentLine += token.text.split('\n').length + 1;
|
type : unmatched.type,
|
||||||
|
err : "Unmatched opening tag"
|
||||||
}));
|
})
|
||||||
|
});
|
||||||
|
|
||||||
return errors;
|
return errors;
|
||||||
},
|
},
|
||||||
marked : Markdown
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* Test Cases
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
30
shared/naturalcrit/validate_test.js
Normal file
30
shared/naturalcrit/validate_test.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
var test =`
|
||||||
|
|
||||||
|
<div class >
|
||||||
|
|
||||||
|
<a>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
<span> </span>
|
||||||
|
|
||||||
|
<div> Hellow !!! </div></div></div></div></div></div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
|
var markdown = require('./markdown.js');
|
||||||
|
|
||||||
|
console.log(markdown.validate(test));
|
||||||
Reference in New Issue
Block a user