diff --git a/source/_posts/c.md b/source/_posts/c.md index cbd23a5..aaa744c 100644 --- a/source/_posts/c.md +++ b/source/_posts/c.md @@ -9,6 +9,7 @@ intro: | C quick reference cheat sheet that provides basic syntax and methods. plugins: - copyCode + - runCode --- ## Getting Started @@ -58,7 +59,7 @@ int y = 6; int sum = x + y; // add variables to sum // declare multiple variables -int x = 5, y = 6, z = 50; +int a = 5, b = 6, c = 50; ``` ### Constants diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index 1c4ae8b..69358e1 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -86,6 +86,10 @@ let language = [...codeBlock.classList].find(c => c.startsWith("language-"))?.replace("language-", "") || ''; if (['', 'shell', 'bash'].includes(language)) return; + if (language === 'c' && content.includes('FILE ')) { + // file operations are not supported in the online playground + return; + } // Create a run button element const runButton = document.createElement('button'); @@ -106,7 +110,7 @@ language = 'javascript'; } - const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl', 'cpp'].includes(language) + const editorId = ['javascript', 'typescript', 'python', 'php', 'ruby', 'go', 'perl', 'cpp', 'c'].includes(language) ? 'script' : ['css', 'scss'].includes(language) ? 'style' @@ -267,6 +271,9 @@ language, content, }, + tools: { + status: 'full', + } } } @@ -291,6 +298,9 @@ language, content, }, + tools: { + status: 'full', + } } } @@ -300,7 +310,7 @@ const freeCode = content.split('\n').filter(line => !line.startsWith('class ') && !line.startsWith(' ') && !line.startsWith('}')).join('\n'); content = `${content.replace(freeCode, '')}\nint main()\n{\n${freeCode.trim().split('\n').map(line => ` ${line}`).join('\n')}\n}`; } else { - content = `int main()\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; + content = `int main()\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; } } if (content.includes('std::') && !content.includes('#include')) { @@ -323,6 +333,32 @@ language: 'cpp-wasm', content, }, + tools: { + status: 'full', + } + } + } + + if (language === 'c') { + if (!content.includes('int main(') && !content.includes('void main(')) { + content = `int main(void)\n{\n${content.trim().split('\n').map(line => ` ${line}`).join('\n')}\n\n return 0;\n}\n`; + } + if ((content.includes('printf') || content.includes('scanf')) && !content.includes('#include ')) { + content = `#include \n\n${content}`; + } + if (content.includes('strcpy') && !content.includes('#include ')) { + content = `#include \n${content}`; + } + config = { + ...baseConfig, + script: { + language: 'cpp-wasm', + title: 'C (Wasm)', + content, + }, + tools: { + status: 'full', + } } }