Skip to content

Commit

Permalink
Merge pull request #110 from UofGAnalytics/fix-python-warning
Browse files Browse the repository at this point in the history
remove python warning from markdown
  • Loading branch information
dmca-glasgow authored May 16, 2022
2 parents 1759354 + 8706822 commit eb17048
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
20 changes: 20 additions & 0 deletions compiler/src/knitr/__test__/knitr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,24 @@ describe('knitr', () => {

expect(ignoreWhitespace(html)).toBe(result);
}, 120000);

it.skip('should remove python warnings from the top of knitr output', async () => {
const { md, messages } = await testProcessor(`
WARNING - All triples will be processed in the same batch (batches_count=1).
When processing large graphs it is recommended to batch the input knowledge
graph instead.
## Introduction
In the first two week of this course we have focused on the use of networks
to analyse social systems, using a mixture of global statistics, node
statistics, and ERGM modelling.
`);

expect(md.trim().startsWith('WARNING')).toBe(false);

expect(messages[0].startsWith('All triples will be processed')).toBe(
true
);
});
});
67 changes: 60 additions & 7 deletions compiler/src/knitr/knitr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { VFile } from 'vfile';

import { Context } from '../context';
import { Unit } from '../course/types';
import { warnMessage } from '../utils/message';
import { infoMessage, warnMessage } from '../utils/message';
import { mkdir, rmFile, writeFile } from '../utils/utils';

// bypass knitr for debugging
Expand Down Expand Up @@ -92,7 +92,7 @@ async function execKnitr(file: VFile, ctx: Context, unitPath: string) {
console.error('ERROR', err);
reject(err);
} else {
reportErrors(response, file);
reportErrors(response, file, ctx);
resolve(formatResponse(response));
}
await rmFile(cachedFile);
Expand Down Expand Up @@ -132,11 +132,59 @@ function getKnitrFileDir() {
return path.dirname(fileURLToPath(import.meta.url));
}

function reportErrors(response: string, file: VFile) {
response.split('\n').forEach((line, idx) => {
const trimmed = line.trim();
if (trimmed.startsWith('## Error')) {
warnMessage(file, trimmed.replace('## ', ''), {
function reportErrors(response: string, file: VFile, ctx: Context) {
const lines = response
.split(EOL)
.filter((s) => !s.startsWith(':directory'));

const trimmed = lines.join(EOL).trim();

// Warning at the start of a document
if (trimmed.startsWith('WARNING -')) {
const match = trimmed.match(/^WARNING - (.+?)[\r\n]{2,}/ms);

// Check the original file doesn't start with WARNING
const original = String(ctx.course.units[0].files[0].value)
.split(EOL)
.filter((s) => !s.startsWith(':directory'))
.join(EOL)
.trim();

if (match !== null && !original.startsWith('WARNING -')) {
warnMessage(file, match[1], {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: lines[0].length,
},
});
}

// Python binary path
} else if (trimmed.startsWith('$python [1]')) {
const match = trimmed.match(/^\$python\s\[1\]\s("\S+")/);
if (match !== null) {
infoMessage(file, match[1], {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: lines[0].length,
},
});
}
}

// Errors throughout document
lines.forEach((line, idx) => {
const trimmedLine = line.trim();
if (trimmedLine.startsWith('## Error')) {
warnMessage(file, trimmedLine.replace('## ', ''), {
start: {
line: idx + 1,
column: 0,
Expand All @@ -153,6 +201,7 @@ function reportErrors(response: string, file: VFile) {
async function formatResponse(response: string) {
let md = response;
md = removeCustomPythonBinNotice(md);
md = removePythonWarningMessage(md);
md = addCodeBlockClasses(md);
md = addErrorCodeBlock(md);
md = removeHashSigns(md);
Expand All @@ -165,6 +214,10 @@ function removeCustomPythonBinNotice(md: string) {
return md.replace(/^\$python\s\[1\]\s"\S+"/, '');
}

function removePythonWarningMessage(md: string) {
return md.replace(/^WARNING - .+?[\r\n]+/m, '');
}

function addCodeBlockClasses(md: string) {
return md
.split('\n')
Expand Down

0 comments on commit eb17048

Please sign in to comment.