This is a web application that allows users to view and copy code snippets for various programming languages. Users can select a language and toggle between different approaches for the same language.
- Display code snippets for multiple programming languages
- Toggle between different approaches for each language
- Copy the selected code snippet to the clipboard
- Syntax highlighting for the code snippets
- HTML
- CSS
- JavaScript
- highlight.js (for syntax highlighting)
The HTML file index.html
contains the structure of the web page. Here's a breakdown of the main elements:
- Header: Displays the title of the web page.
- Language Buttons: Allows users to select a programming language.
- Code Container: Holds the code snippet and the "Approach" buttons.
- Code Block Selector: Displays the "Approach" buttons when multiple approaches are available for a language.
- Code Blocks: Holds the code snippets for each approach.
- Copy Button: Enables users to copy the selected code snippet.
- Footer: Displays the copyright information.
The CSS file styles.css
defines the visual appearance of the web page. It includes styles for:
- Body: Sets the background color and text color.
- Header: Styles the header section.
- Container: Centers the content and sets the maximum width.
- Language Buttons: Styles the language selection buttons.
- Code Container: Styles the code snippet container.
- Code Block Selector: Styles the "Approach" buttons container.
- Code Buttons: Styles the individual "Approach" buttons.
- Copy Button: Styles the "Copy" button.
- Syntax Highlighting: Overrides the highlight.js default background color.
- Footer: Styles the footer section.
The JavaScript file script.js
contains the logic to handle user interactions and update the displayed code snippets. Here's a breakdown of the main functions:
showCode(language)
: Displays the code snippets for the selected language and updates the "Approach" buttons based on the available approaches.showCodeBlock(index)
: Displays the selected code block based on the "Approach" button clicked.copyCode()
: Copies the currently visible code snippet to the clipboard.getCurrentLanguage()
: Retrieves the currently selected language.
When the page loads, the showCode('c')
function is called to display the C language code snippet and the first approach.
- Open the
index.html
file in a web browser. - Select a programming language from the language buttons.
- If multiple approaches are available for the selected language, use the "Approach" buttons to toggle between them.
- Click the "Copy" button to copy the currently visible code snippet to the clipboard.
To customize the code snippets or add new languages, you can modify the codes
object in the script.js
file. Each language is represented as a key in the object, and its value is an array of code snippets for each approach.
For example, to add a new language:
const codes = {
// ...
ruby: [
`def array
# Ruby code for array
end`,
`def array
# Ruby code for array - Approach 2
end`
]
// ...
};
Then, add a new language button in the index.html
file:
<button class="lang-btn" onclick="showCode('ruby')">Ruby</button>
This code snippet viewer allows users to easily access and copy code snippets for various programming languages. It provides a clean and intuitive interface for toggling between different approaches and copying the desired code snippet.