Skip to content

Commit

Permalink
add code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
chn-lee-yumi committed Feb 21, 2024
1 parent b786563 commit 2dca5f6
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
This repository hosts the content for [tool.gcc.ac.cn](https://tool.gcc.ac.cn).

The toolbox includes:
- [Base64 Encoding/Decoding](https://tool.gcc.ac.cn/en/base64.html)
- [Number Base Conversion](https://tool.gcc.ac.cn/en/base_converter.html)
- [FFmpeg in Browser](https://ffmpeg.gcc.ac.cn/en/index.html) [Repository Link](https://github.com/chn-lee-yumi/ffmpeg_in_browser)
- [ASCII Code Conversion](https://tool.gcc.ac.cn/en/ascii.html)
- [Number Base Conversion](https://tool.gcc.ac.cn/en/base_converter.html)
- [Base64 Encoding/Decoding](https://tool.gcc.ac.cn/en/base64.html)
- [UTF-8 Conversion](https://tool.gcc.ac.cn/en/utf8.html)
- [FFmpeg in Browser](https://ffmpeg.gcc.ac.cn/en/index.html) [Repository Link](https://github.com/chn-lee-yumi/ffmpeg_in_browser)
- [Word Counter & Character Counter](https://tool.gcc.ac.cn/en/word_count.html)
- [Least Common Multiple (LCM) Calculator](https://tool.gcc.ac.cn/en/lcm.html)
- [Code Formatter (Supports JavaScript and HTML)](https://tool.gcc.ac.cn/en/formatter.html)
- [Show My IP Address](https://ip.gcc.ac.cn/)
9 changes: 6 additions & 3 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
本仓库为 [tool.gcc.ac.cn](https://tool.gcc.ac.cn) 的页面内容托管。

工具箱内容包含:
- [base64编解码](https://tool.gcc.ac.cn/zh/base64.html)
- [进制转换](https://tool.gcc.ac.cn/zh/base_converter.html)
- [网页版FFmpeg](https://ffmpeg.gcc.ac.cn/index_zh.html) [仓库地址](https://github.com/chn-lee-yumi/ffmpeg_in_browser)
- [ASCII码转换](https://tool.gcc.ac.cn/zh/ascii.html)
- [进制转换](https://tool.gcc.ac.cn/zh/base_converter.html)
- [base64编解码](https://tool.gcc.ac.cn/zh/base64.html)
- [UTF-8编码转换](https://tool.gcc.ac.cn/zh/utf8.html)
- [网页版FFmpeg](https://ffmpeg.gcc.ac.cn/index_zh.html) [仓库地址](https://github.com/chn-lee-yumi/ffmpeg_in_browser)
- [字数和单词统计](https://tool.gcc.ac.cn/zh/word_count.html)
- [最小公倍数计算](https://tool.gcc.ac.cn/zh/lcm.html)
- [代码格式化(支持JavaScript和HTML)](https://tool.gcc.ac.cn/zh/formatter.html)
- [查看我的 IP 地址](https://ip.gcc.ac.cn/)
131 changes: 131 additions & 0 deletions en/formatter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="An online code formatter, supports JavaScript and HTML. Online JS Formatter. Online HTML Formatter.">
<title>Online Code Formatter</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f0f0f0;
text-align: center;
margin: 20px;
}

h1 {
color: #333;
}

textarea {
width: 80%;
height: 300px;
margin: 10px;
padding: 10px;
font-size: 14px;
}

button {
background-color: #4caf50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #45a049;
}

#outputCode {
width: 80%;
height: 300px;
margin: 10px;
padding: 10px;
font-size: 14px;
background-color: #fff;
color: #333;
border: 1px solid #ccc;
border-radius: 5px;
}

select {
padding: 5px;
font-size: 14px;
}
</style>
</head>

<body>
<h1>Code Formatter</h1>
<label for="language">Select language:</label>
<select id="language">
<option value="js">JavaScript</option>
<option value="html">HTML</option>
</select>
<br>
<textarea id="inputCode" placeholder="Enter your code here..."></textarea>
<br>
<button onclick="formatCode()">Format Code</button>
<button onclick="copyToClipboard()">Copy Output</button>
<br>
<textarea id="outputCode" placeholder="Formatted code will appear here..." readonly></textarea>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-css.min.js"></script>
<script>
var options = {
"indent_size": "4",
"indent_char": " ",
"max_preserve_newlines": "2",
"preserve_newlines": true,
"keep_array_indentation": false,
"break_chained_methods": false,
"indent_scripts": "normal",
"brace_style": "collapse",
"space_before_conditional": true,
"unescape_strings": false,
"jslint_happy": false,
"end_with_newline": false,
"wrap_line_length": "0",
"indent_inner_html": false,
"comma_first": false,
"e4x": false,
"indent_empty_lines": false
};

function formatCode() {
var inputCode = document.getElementById('inputCode').value;
var selectedLanguage = document.getElementById('language').value;
var outputCode;

if (selectedLanguage === 'js') {
outputCode = beautifyJavaScript(inputCode);
} else if (selectedLanguage === 'html') {
outputCode = beautifyHTML(inputCode);
}

document.getElementById('outputCode').value = outputCode;
}

function beautifyJavaScript(code) {
return js_beautify(code, options);
}

function beautifyHTML(code) {
return html_beautify(code, options);
}

function copyToClipboard() {
var outputTextarea = document.getElementById('outputCode');
outputTextarea.select();
document.execCommand('copy');
}
</script>
</body>

</html>
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ <h2>Tools list:</h2>
<li><a href="en/utf8.html" target="_blank">UTF-8 Encoding/Decoding</a></li>
<li><a href="en/word_count.html" target="_blank">Word Counter & Character Counter</a></li>
<li><a href="en/lcm.html" target="_blank">Least Common Multiple (LCM) Calculator</a></li>
<li><a href="en/formatter.html" target="_blank">Code Formatter (Supports JavaScript and HTML)</a></li>
<li><a href="https://ip.gcc.ac.cn/" target="_blank">Show My IP Address</a></li>
</ul>
</div>
Expand Down
3 changes: 2 additions & 1 deletion index_zh.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand Down Expand Up @@ -65,6 +65,7 @@ <h2>工具列表:</h2>
<li><a href="zh/utf8.html" target="_blank">UTF-8 编解码</a></li>
<li><a href="zh/word_count.html" target="_blank">字数和单词统计</a></li>
<li><a href="zh/lcm.html" target="_blank">最小公倍数计算</a></li>
<li><a href="zh/formatter.html" target="_blank">代码格式化(支持JavaScript和HTML)</a></li>
<li><a href="https://ip.gcc.ac.cn/" target="_blank">查看我的 IP 地址</a></li>
</ul>
</div>
Expand Down
4 changes: 3 additions & 1 deletion sitemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ https://tool.gcc.ac.cn/en/base_converter.html
https://tool.gcc.ac.cn/en/utf8.html
https://tool.gcc.ac.cn/en/word_count.html
https://tool.gcc.ac.cn/en/lcm.html
https://tool.gcc.ac.cn/en/formatter.html
https://tool.gcc.ac.cn/zh/ascii.html
https://tool.gcc.ac.cn/zh/base64.html
https://tool.gcc.ac.cn/zh/base_converter.html
https://tool.gcc.ac.cn/zh/utf8.html
https://tool.gcc.ac.cn/zh/word_count.html
https://tool.gcc.ac.cn/zh/lcm.html
https://tool.gcc.ac.cn/zh/lcm.html
https://tool.gcc.ac.cn/zh/formatter.html
131 changes: 131 additions & 0 deletions zh/formatter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="在线代码格式化,支持 JavaScript 和 HTML。在线JS格式化,在线HTML格式化。">
<title>在线代码格式化</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f0f0f0;
text-align: center;
margin: 20px;
}

h1 {
color: #333;
}

textarea {
width: 80%;
height: 300px;
margin: 10px;
padding: 10px;
font-size: 14px;
}

button {
background-color: #4caf50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #45a049;
}

#outputCode {
width: 80%;
height: 300px;
margin: 10px;
padding: 10px;
font-size: 14px;
background-color: #fff;
color: #333;
border: 1px solid #ccc;
border-radius: 5px;
}

select {
padding: 5px;
font-size: 14px;
}
</style>
</head>

<body>
<h1>代码格式化</h1>
<label for="language">选择语言:</label>
<select id="language">
<option value="js">JavaScript</option>
<option value="html">HTML</option>
</select>
<br>
<textarea id="inputCode" placeholder="填入你的代码..."></textarea>
<br>
<button onclick="formatCode()">Format Code</button>
<button onclick="copyToClipboard()">Copy Output</button>
<br>
<textarea id="outputCode" placeholder="格式化后的代码会显示在这里..." readonly></textarea>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-css.min.js"></script>
<script>
var options = {
"indent_size": "4",
"indent_char": " ",
"max_preserve_newlines": "2",
"preserve_newlines": true,
"keep_array_indentation": false,
"break_chained_methods": false,
"indent_scripts": "normal",
"brace_style": "collapse",
"space_before_conditional": true,
"unescape_strings": false,
"jslint_happy": false,
"end_with_newline": false,
"wrap_line_length": "0",
"indent_inner_html": false,
"comma_first": false,
"e4x": false,
"indent_empty_lines": false
};

function formatCode() {
var inputCode = document.getElementById('inputCode').value;
var selectedLanguage = document.getElementById('language').value;
var outputCode;

if (selectedLanguage === 'js') {
outputCode = beautifyJavaScript(inputCode);
} else if (selectedLanguage === 'html') {
outputCode = beautifyHTML(inputCode);
}

document.getElementById('outputCode').value = outputCode;
}

function beautifyJavaScript(code) {
return js_beautify(code, options);
}

function beautifyHTML(code) {
return html_beautify(code, options);
}

function copyToClipboard() {
var outputTextarea = document.getElementById('outputCode');
outputTextarea.select();
document.execCommand('copy');
}
</script>
</body>

</html>

0 comments on commit 2dca5f6

Please sign in to comment.