├── .gitattributes
├── .github
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── .gitignore
├── .vscode
├── CSPSH_snippets.code-snippets
└── settings.json
├── LICENSE
├── README.md
├── Resources
├── CSPSH(withGrad).png
└── CSPSH.png
├── Source
├── CSPSH.js
├── CSPSH
│ ├── HighLight.js
│ ├── SyntaxHighlight.js
│ └── TabBuilder.js
├── LangTokens
│ ├── CSPSHC.js
│ ├── CSPSHCPP.js
│ ├── CSPSHCSS.js
│ ├── CSPSHHTML.js
│ ├── CSPSHJAVA.js
│ ├── CSPSHJS.js
│ ├── CSPSHPY.js
│ └── CSPSHSS.js
└── ThemeStyles
│ ├── CSPSHDARK.css
│ ├── DRAKULADARK.css
│ └── MONOKAIDARK.css
├── index.css
├── index.html
└── index.js
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Node rules:
2 | ## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
3 | .grunt
4 |
5 | ## Dependency directory
6 | ## Commenting this out is preferred by some people, see
7 | ## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
8 | node_modules
9 | *.ini
10 |
11 | # Book build output
12 | _book
13 |
14 | # eBook build output
15 | *.epub
16 | *.mobi
17 | *.pdf
18 |
--------------------------------------------------------------------------------
/.vscode/CSPSH_snippets.code-snippets:
--------------------------------------------------------------------------------
1 | {
2 | "Initialize CSPSH": {
3 | "scope": "javascript, typescript",
4 | "prefix": "cspsh",
5 | "body": [
6 | "const cspsh = new CSPSH;",
7 | ],
8 | "description": "Initialize CSPSH class"
9 | },
10 |
11 | "with user defined variable name":{
12 | "scope": "javascript, typescript",
13 | "prefix": "cspsh",
14 | "body": [
15 | "const $1 = new CSPSH;",
16 | ],
17 | "description": "with user defined variable name"
18 | },
19 |
20 | "with user defined variable name and default options":{
21 | "scope": "javascript, typescript",
22 | "prefix": "cspsh",
23 | "body": [
24 | "const $1 = new CSPSH;",
25 | "$1.highlight($1.defaultOitons());"
26 | ],
27 | "description": "with user defined variable name and default options"
28 | },
29 |
30 | "with user defined variable name and user defined options":{
31 | "scope": "javascript, typescript",
32 | "prefix": "cspsh",
33 | "body": [
34 | "const $1 = new CSPSH;",
35 | "$1.highlight({",
36 | " file: $2,",
37 | " highlightLine: [$3],",
38 | " SourcePath: \"$4\"",
39 | "});"
40 | ],
41 | "description": "with user defined variable name and default options"
42 | },
43 |
44 | "use highlight method with default options":{
45 | "scope": "javascript, typescript",
46 | "prefix": "cspsh",
47 | "body": [
48 | "cspsh.highlight(cspsh.deafultOptions());"
49 | ],
50 | "description": ".highlight method with default options"
51 | },
52 |
53 | "use highlight method with user defined options":{
54 | "scope": "javascript, typescript",
55 | "prefix": "cspsh",
56 | "body": [
57 | "const cspsh = new CSPSH",
58 | "cspsh.highlight({",
59 | " file: \"$1\",",
60 | " highlightLine: [$2],",
61 | " SourcePath: \"$3\"",
62 | "})",
63 | ],
64 | "description": "with user defined options"
65 | },
66 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "liveServer.settings.port": 5501,
3 | "python.analysis.typeCheckingMode": "basic"
4 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 chandu-programmer
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CSPSH
2 |
3 | 
4 | A syantax highlighting library (or) package for highlighting your codes in webpages.
5 |
6 | 
7 | 
8 | 
9 | 
10 | 
11 |
12 | - Yo help me in adding feature. Well simply comment which feature you want I will try to add. Because *cough* I can't get any feature ideas. Thank you 🙏
13 |
14 | # Steps to use:
15 |
16 | - Download the library and include the script using script tag as shown below or import into your js file
17 |
18 | ```html
19 |
20 |
21 |
22 |
23 | ```
24 |
25 | or
26 |
27 | ```js
28 | import {CSPSH} from 'path to the CSPSH.js file'
29 | ```
30 |
31 | - It is recommended to place the script tag at the end of your ``body`` tag in html.
32 | **NOTE: Make sure to place the Source folder of this repo into your directory where your main html file is present (or) paste the exact path of CSPSH.js. But I recommend to copy the source folder to your work folder because CSPSH.js will automatically add all the required css files once the page is loaded.**
33 |
34 | - In HTML define a class name with ``CSPSH`` in order to make CSPSH.js realise the code that is written in that element(I personally prefer div tag for that) and theme attribute to either ``monokai`` or ``drakula`` or default theme ``cspsh`` to choose the theme scheme and lang is the language of the code that is written inside the tag/element. see the example below for better understanding.
35 |
36 | ```html
37 |
38 | ```
39 |
40 | - Start typing your code inside this tag.
41 |
42 | ```html
43 |
44 |
48 | ```
49 |
50 | - After typing your code go to your JS file and create a ``window.onload`` event and inside ``window.onload`` create an object for ``CSPSH`` class. In previous versions this is not required becasue this package don't take any user options from the JS file but as two new features were added options are compulsory.
51 | - For more information about the options please visit the [CSPSH documentation](https://csp02/The-Atelier/Pages/CSPSH/cspshDocs.html).
52 | - Now run the highlight method inside CSPSH object using the ``.highlight(options)`` method where options can be user defined as shown in docs.
53 |
54 | # Major Version info:
55 | 1. Added support for python(but python code cannot be formatted by cspsh itself user have to format but for other languages cspsh can format)
56 | 2. Added support for css.
57 | 3. Fixed some known bugs
58 | 4. Changed the theme palette for cspsh theme
59 |
60 | # Minor version info
61 | 1. Added simple mode which will *yeet* i mean remove all the fancy stuff and shows code in simple way.
62 | 2. Added multiple file multiple linehighlight...sounds confusing yes well lemme elaborate. In the previous versions users can't highlight specific lines for multiple files. But it is no more in this update I added this new feature.
63 | 3. Optimized some code.
64 |
65 | # Optimizations:
66 | - Accurate copy to clipboard functionality
67 | - Accurate line count functionality
68 | - Improved scrolling and fixed a bug where the scroll bar of whole document changes. Now the scrollbar changes only for CSPSH code.
69 |
70 | **Planned improvements**
71 | - Add some more themes.
72 | - Add more language support. well done by giving c lang highlighting for any other langs.
73 |
74 | **Example**
75 |
76 | ```html
77 |
78 | //single line comments
79 | /*multi
80 | line
81 | comments*/
82 | const toggler = new Toggler
83 | toggler.toggleClass ('myId', 'fromClass', 'toClass')
84 | var a = 1
85 | var b = 2
86 | console.log(biggest(a, b))
87 | function biggest(a, b) {
88 | if (a < b){
89 | return b
90 | } else {
91 | return a
92 | }
93 | }
94 |
95 | ```
96 | **Note:**
97 | - User must define the window.onload event in the JS file and create an object for CSPSH and run ``highlight(options)`` method. If user doesn't define the window.onload event in the JS file then the code will not get highlighted.
98 | > script.js
99 | ```js
100 | window.onload = function(){
101 | //code that user wants to perform
102 | const cspsh = new CSPSH
103 | const options = {
104 | file: ['file.extension'],
105 | highlightLine: [[12, 20, 22]],
106 | SourcePath: '/Source'
107 | }
108 | cspsh.highlight(options)
109 | }
110 | ```
111 | - This will automatically highlight the code which has CSPSH as class name for more info visit [docs](https://csp02/The-Atelier/Pages/CSPSH/cspshDocs.html).
112 | **Link for story script(extention is sts)**
113 | - StoryScript is created and developed by @lines-of-codes using python. [Learn more](https://github.com/StoryScriptorg/StoryScript/tree/main/storyscript).
114 |
115 | - Join our discord server for any queries [the-atelier](https://discord.gg/6Mcy5NpSpH)
116 |
117 | **NOTE: It is not recommended to format your HTML Code with tab spaces(unless it's python code) as its not necessary. But you must use new line(enter or a semicolon) to make it identify the new lines.**
--------------------------------------------------------------------------------
/Resources/CSPSH(withGrad).png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CSP02/CSPSH/008c7b16829fed8fde11c7bb7897bd7b375de7d2/Resources/CSPSH(withGrad).png
--------------------------------------------------------------------------------
/Resources/CSPSH.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CSP02/CSPSH/008c7b16829fed8fde11c7bb7897bd7b375de7d2/Resources/CSPSH.png
--------------------------------------------------------------------------------
/Source/CSPSH.js:
--------------------------------------------------------------------------------
1 | //importing all the tokens of each language
2 | import { JSTOKENS } from './LangTokens/CSPSHJS.js'
3 | import { CPPTOKENS } from './LangTokens/CSPSHCPP.js'
4 | import { CTOKENS } from './LangTokens/CSPSHC.js'
5 | import { JAVATOKENS } from './LangTokens/CSPSHJAVA.js'
6 | import { SSTOKENS } from './LangTokens/CSPSHSS.js'
7 | import { HTMLTOKENS } from './LangTokens/CSPSHHTML.js'
8 | import { CSSTOKENS } from './LangTokens/CSPSHCSS.js'
9 | import { PYTOKENS } from './LangTokens/CSPSHPY.js'
10 |
11 | //importing other files that required cspsh
12 | import { HighLight } from './CSPSH/HighLight.js'
13 | import { BuildTabs } from './CSPSH/TabBuilder.js'
14 |
15 | //Initializing all the required variables and constants.
16 | let content = []
17 | let openBraceCount = 0
18 | let fileName
19 | let isEOLINFOR
20 | let forCount
21 | let lang
22 | let theme = 'cspsh'
23 | let themesList = []
24 | let tempStr = []
25 | let lineCountHolder
26 | let lineCount = 0
27 | let code
28 | let params
29 | let links = [...document.head.getElementsByTagName('link')]
30 | let codeAndLineCount
31 | let tempClassName
32 | let codeHolder
33 | let codeContent
34 |
35 | //the following block will move the user's css to the down
36 | for (let h = 0; h < links.length; h++) {
37 | let link = links[h]
38 | link.parentElement.removeChild(link)
39 | }
40 | const copySvg = `
41 | `
48 |
49 | export class CSPSH {
50 | //Runs right after the page is loaded
51 | highlight(options) {
52 | try {
53 | let codes = document.getElementsByClassName('CSPSH');
54 | let sourcePath = options.SourcePath;
55 | let mode = options.mode;
56 | if (options.SourcePath === 'default')
57 | sourcePath = '/Source'
58 | document.head.innerHTML += ``
59 | document.head.innerHTML += ``
60 | document.head.innerHTML += ``
61 |
62 | //Reads the required attributes and links the required stylesheets based on selected theme
63 | for (let i = 0; i < codes.length; i++) {
64 | codeHolder = codes[i]
65 | fileName = codeHolder.getAttribute('name')
66 | codeHolder.className += ` ${codeHolder.getAttribute('theme').toUpperCase()}`
67 | if (codeHolder.getAttribute('theme') && !themesList.includes(codeHolder.getAttribute('theme'))) {
68 | if (codeHolder.getAttribute('theme').split(' ').length > 1)
69 | theme = codeHolder.getAttribute('theme').split(' ')[0];
70 | else
71 | theme = codeHolder.getAttribute('theme')
72 | themesList.push(theme)
73 | } else
74 | theme = codeHolder.getAttribute('theme')
75 | if (!fileName) {
76 | fileName = `file`//sets default file name to file
77 | }
78 | //push all the code content.
79 | content.push(codeHolder.textContent)
80 | codeContent = codeHolder.textContent
81 | //Detect the language and start highlighting
82 | switch (codeHolder.lang) {
83 | case 'js' || 'ts':
84 | lang = new JSTOKENS()
85 | break
86 | case 'cpp':
87 | lang = new CPPTOKENS()
88 | break
89 | case 'c':
90 | lang = new CTOKENS()
91 | break
92 | case 'java' || 'c#':
93 | lang = new JAVATOKENS()
94 | break
95 | case 'sts':
96 | lang = new SSTOKENS()
97 | lineCount = 0
98 | break
99 | case 'css':
100 | lang = new CSSTOKENS();
101 | lineCount = 0
102 | break
103 | case 'html':
104 | lang = new HTMLTOKENS();
105 | lineCount = 0;
106 | break
107 | case 'py':
108 | lang = new PYTOKENS();
109 | lineCount = 0;
110 | break
111 | default:
112 | lang = new CTOKENS();
113 | lineCount = 0;
114 | }
115 | Start(options);
116 | }
117 | //appends the link tags back to head so that user can manipulate the properties of cspsh.
118 | links.forEach(link => {
119 | document.head.appendChild(link)
120 | })
121 |
122 | //Copy to clipboard functionlity inside the HighLight() method
123 | const buttons = document.getElementsByClassName('copyVector')
124 | //Adds the copy to clipboard functionality to all the buttons
125 | for (let j = 0; j < buttons.length; j++) {
126 | const button = buttons[j];
127 | button.addEventListener('click', function () {
128 | //get the innertext of #code element i.e the code to be copied
129 | const clipboardText = button.parentElement.parentElement.lastChild.innerText
130 | const copiedSvg = button.children[0];
131 | //copy the code to clipboard
132 | navigator.clipboard.writeText(clipboardText)
133 | //change the svg to tick mark indicating that the code is copied
134 | copiedSvg.innerHTML = ``
136 | //wait for 0.5s (500ms) and revert back to the original svg
137 | setTimeout(function () {
138 | copiedSvg.innerHTML = copySvg
139 | }, 500)
140 | })
141 | }
142 | //End of copy to clipboard functionality
143 | const retroEffects = document.getElementsByClassName('retro_effect');
144 | [...retroEffects].forEach(retroEffect => {
145 | retroEffect.addEventListener('input', function () {
146 | const cspshHolder = retroEffect.parentElement.parentElement.parentElement.parentElement;
147 | cspshHolder.classList.toggle('retro')
148 | const retroInputSib = retroEffect.nextSibling
149 | const retroCircle = retroInputSib.firstChild
150 | if(retroEffect.checked){
151 | retroInputSib.style.backgroundColor = "green"
152 | retroCircle.style.marginLeft = "1.8rem"
153 | }else{
154 | retroInputSib.style.backgroundColor = "#dcdcdc"
155 | retroCircle.style.marginLeft = "0"
156 | }
157 | })
158 | })
159 | //start of theme changing
160 | const themeSelectors = document.getElementsByClassName("theme_selectors");
161 | [...themeSelectors].forEach(themeSelector => {
162 | const theme = themeSelector.parentElement.parentElement.parentElement.getAttribute('theme');
163 | [...themeSelector.options].forEach(option => {
164 | theme === option.value ? option.setAttribute('selected', 'true') : option.removeAttribute('selected');
165 | });
166 | themeSelector.addEventListener("change", changeData => {
167 | const theme = themeSelector.parentElement.parentElement.parentElement.getAttribute('theme');
168 | [...themeSelector.options].forEach(option => {
169 | theme === option.value ? option.setAttribute('selected', 'true') : option.removeAttribute('selected');
170 | })
171 | themeSelector.parentElement.nextSibling.className = `themeusing${themeSelector.value}`;
172 | themeSelector.parentElement.parentElement.className = `copyHolder-${themeSelector.value}`;
173 | themeSelector.parentElement.parentElement.parentElement.classList.remove(theme.toUpperCase());
174 | themeSelector.parentElement.parentElement.parentElement.classList.add(themeSelector.value.toUpperCase());
175 | const cspshTags = [...themeSelector.parentElement.parentElement.parentElement.getElementsByTagName('cspsh')]
176 | const divs = [...themeSelector.parentElement.parentElement.parentElement.getElementsByTagName('div')]
177 | divs.forEach(div => {
178 | div.className = div.className.replaceAll(`${theme}`, `${themeSelector.value}`);
179 | })
180 | cspshTags.forEach(cspsh => {
181 | if (cspsh.className.split('-')[0] === 'sh')
182 | cspsh.className = cspsh.className.replaceAll(`${theme}`, `${themeSelector.value}`);
183 | })
184 | const lineHighlights = [...themeSelector.parentElement.parentElement.parentElement.lastChild.getElementsByClassName(`sh-${theme}-lineHighlight`)];
185 | lineHighlights.forEach(lineHighlight => {
186 | lineHighlight.className = lineHighlight.className.replaceAll(`sh-${theme.toUpperCase()}-lineHighlight`, `sh-${themeSelector.value.toUpperCase()}-lineHighlight`);
187 | })
188 |
189 | const lineCountHolder = themeSelector.parentElement.parentElement.parentElement.lastChild.getElementsByClassName('linecount-holder')[0]
190 | lineCountHolder.classList.remove(`lineCount-${theme.toUpperCase()}-done`)
191 | lineCountHolder.classList.add(`lineCount-${themeSelector.value.toUpperCase()}-done`)
192 | const mainHolder = themeSelector.parentElement.parentElement.parentElement;
193 | mainHolder.setAttribute('theme', themeSelector.value);
194 | themeSelector.parentElement.nextSibling.innerText = 'Current Theme: ' + themeSelector.value.toUpperCase()
195 | })
196 | })
197 | //end of theme changing
198 | //Build tabs if there are any
199 | BuildTabs(mode);
200 | //auto-scroll the linecount according to the scroll position of the code
201 | AutoScroll();
202 | //add titles so when user hovers on the tokens it shows the token name
203 | AddTitles();
204 | } catch (e) {
205 | codeHolder.lastChild.innerHTML += `
206 |
!unable to highlight code due to some issue.
207 |
Here is the full details:
${e.stack.toString().split('\n').join(' ')}
208 | you can report the Error by clicking reporting an issue here:
209 | Report a bug
210 |
Please use the issue template provided while reporting an issue.
43 | const a = 10;
44 | const b = 20;
45 | let c = add(a, b);
46 | console.log(c);
47 |
48 | function add(a, b) {
49 | console.log("retro effect!")
50 | return a + b;
51 | }
52 |
53 |
54 |
55 | Without Code Tabs:
56 |
57 |
58 | def add(a, b):
59 | return a + b
60 | a = 10
61 | b = 20
62 | c = add(a, b)
63 | print(c)
64 |
65 |
66 | public class Main {
67 | public static void main(String[] args) {
68 | int a = 10;
69 | int b = 20;
70 | int c = add(a, b);
71 | System.out.println(c);
72 | }
73 | public static int add(int a, int b) {
74 | return a + b;
75 | }
76 | }
77 |
78 |
79 | //storyscript is a language developed by lines-of-code. You can search story script in github to know more
80 |
81 | int a = 10
82 | int b = 20
83 |
84 | print(a + b)
85 |
86 |
87 | #include <stdio.h>
88 |
89 | int main() {
90 | int a = 10;
91 | int b = 20;
92 | int c = add(a, b);
93 | printf("%d", c);
94 | return 0;
95 | }
96 |
97 | int add(int a, int b) {
98 | return a + b;
99 | }
100 |
101 |
102 | const a = 10;
103 | const b = 20;
104 | let c = add(a, b);
105 | console.log(c, "simple mode");
106 |
107 | function add(a, b) {
108 | return a + b;
109 | }
110 |