mirror of
https://github.com/boostorg/boostlook.git
synced 2026-02-27 17:02:11 +00:00
1 line
2.8 KiB
JavaScript
1 line
2.8 KiB
JavaScript
const CppHighlight=function(){"use strict";const o=new Set(["auto","register","static","extern","mutable","thread_local","const","volatile","constexpr","consteval","constinit","void","bool","char","short","int","long","float","double","signed","unsigned","wchar_t","char8_t","char16_t","char32_t","class","struct","union","enum","typename","typedef","if","else","switch","case","default","for","while","do","break","continue","return","goto","try","catch","throw","noexcept","public","private","protected","virtual","override","final","friend","this","operator","new","delete","template","concept","requires","namespace","using","sizeof","alignof","alignas","decltype","typeid","static_cast","dynamic_cast","const_cast","reinterpret_cast","static_assert","inline","explicit","export","module","import","co_await","co_yield","co_return","true","false","nullptr","NULL"]);function a(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function u(e,t){return`<span class="cpp-${e}">${a(t)}</span>`}function t(i){var r=[];let l=0;for(var n=i.length;l<n;)if("["===i[l]&&"["===i[l+1]){var t=l;l+=2;let e=1;for(;l<n&&0<e;)"["===i[l]&&"["===i[l+1]?(e++,l+=2):"]"===i[l]&&"]"===i[l+1]?(e--,l+=2):l++;r.push(u("attribute",i.slice(t,l)))}else if("/"===i[l]&&"/"===i[l+1]){let e=l+2;for(;e<n&&"\n"!==i[e];)e++;r.push(u("comment",i.slice(l,e))),l=e}else if("/"===i[l]&&"*"===i[l+1]){let e=l+2;for(;e<n-1&&("*"!==i[e]||"/"!==i[e+1]);)e++;e+=2,r.push(u("comment",i.slice(l,e))),l=e}else{if("#"===i[l]){let e=l-1;for(;0<=e&&(" "===i[e]||"\t"===i[e]);)e--;if(e<0||"\n"===i[e]){let e=l+1;for(;e<n;){if("\n"===i[e])if("\\"!==i[e-1])break;e++}r.push(u("preprocessor",i.slice(l,e))),l=e;continue}}if("R"===i[l]&&'"'===i[l+1]){let e=l+2;for(;e<n&&"("!==i[e];)e++;var s=")"+i.slice(l+2,e)+'"';let t=e+1;for(;t<n;){if(i.slice(t,t+s.length)===s){t+=s.length;break}t++}r.push(u("string",i.slice(l,t))),l=t}else if('"'===i[l]||("L"===i[l]||"u"===i[l]||"U"===i[l])&&'"'===i[l+1]||"u"===i[l]&&"8"===i[l+1]&&'"'===i[l+2]){t=l;for("u"===i[l]&&"8"===i[l+1]?l+=2:'"'!==i[l]&&l++,l++;l<n&&'"'!==i[l];)"\\"===i[l]&&l+1<n?l+=2:l++;l++,r.push(u("string",i.slice(t,l)))}else if("'"===i[l]||("L"===i[l]||"u"===i[l]||"U"===i[l])&&"'"===i[l+1]||"u"===i[l]&&"8"===i[l+1]&&"'"===i[l+2]){var c=l;for("u"===i[l]&&"8"===i[l+1]?l+=2:"'"!==i[l]&&l++,l++;l<n&&"'"!==i[l];)"\\"===i[l]&&l+1<n?l+=2:l++;l++,r.push(u("string",i.slice(c,l)))}else if(/[a-zA-Z_]/.test(i[l])){let e=l+1;for(;e<n&&/[a-zA-Z0-9_]/.test(i[e]);)e++;c=i.slice(l,e);o.has(c)?r.push(u("keyword",c)):r.push(a(c)),l=e}else r.push(a(i[l])),l++}return r.join("")}function i(e){e.innerHTML=t(e.textContent)}return{highlight:t,highlightElement:i,highlightAll:function(e="code.cpp, code.c++, pre.cpp, pre.c++"){document.querySelectorAll(e).forEach(i)},KEYWORDS:o}}();"undefined"!=typeof module&&module.exports&&(module.exports=CppHighlight); |