Skip to content

Commit cd2557d

Browse files
sourabhtk37nishutosh
authored andcommitted
Added Syntax highlighting
- Lanugage detection using 'highlight.js' - Editing and syntax highlighting using 'codemirror.js' - 'neo' theme of codemirror is used - 'loadmode.js' to change the syntax highlighting Minor changes: - Removed contrib.messages as it's not needed - Updated Tests
1 parent 41f65e6 commit cd2557d

File tree

16 files changed

+10953
-88
lines changed

16 files changed

+10953
-88
lines changed

CONTRIBUTING.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
## Contributing
12

2-
### Contributing
3+
Contribution are welcome as always. Checkout the existing issues or create issues if you encounter any problem with the project.
34

4-
Contribution are welcome as always. Checkout the existing issues or create issues.
5+
### Steps for contributing
56

6-
Steps for contributing:
7+
#### 1. Making Changes
78

8-
- Fork and clone the repository
9+
- Fork and clone the repository.
910

10-
- If resolving an issue then create a branch with name like `bugfix-#<issue_number>` or `enhancement-#<issue_number>`
11+
- Always branch from updated `development` branch locally.
1112

12-
- After commiting, push the branch to your upstream fork.
1313

14-
- Create a Pull request to this repository
14+
- If resolving an issue then create a branch with name like `bugfix-#<issue_number>` or `enhancement-#<issue_number>`.
15+
16+
- Write a commit messages describing the changes you made
17+
18+
#### 2. Submitting Pull request
19+
20+
- Rebase your commits with latest changes from `development`.
21+
22+
- Push your changes to your upstream fork.
23+
24+
- Create a pull request to the `development` branch of the repository.
25+

code_share/app_code_share/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CodeShare(models.Model):
1010
:field code: text field for code content
1111
:field hash_value: unique hash ID for distinguishing code snipeets
1212
:field file_name: character field for file name
13+
:field language: type of programming language
1314
1415
1516
"""
@@ -28,6 +29,11 @@ class CodeShare(models.Model):
2829
null=True,
2930
blank=True)
3031

32+
language = models.CharField(max_length=20,
33+
null=True,
34+
blank=True,
35+
default=None)
36+
3137
def __unicode__(self):
3238
return str(self.hash_value)
3339

code_share/app_code_share/templates/app_code_share/base.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{% load staticfiles %}
21
{% include "fork_github.html" %}
2+
{% load staticfiles %}
33

44
<!DOCTYPE html>
55

@@ -11,6 +11,13 @@
1111
<title>Shitty Code Share</title>
1212
<link rel="stylesheet" href="{% static 'css/normalize.css' %}">
1313
<link rel="stylesheet" href="{% static 'css/milligram.css' %}">
14+
<link rel="stylesheet" href="{% static 'css/codemirror.css' %}"/>
15+
<link rel="stylesheet" href="{% static 'css/neo.css' %}" />
16+
17+
<script type="text/javascript" src="{% static 'js/codemirror.js' %}"></script>
18+
<script type="text/javascript" src="{% static 'js/highlight.js' %}"></script>
19+
<script type="text/javascript" src="{% static 'js/loadmode.js' %}"></script>
20+
1421

1522
{% block css %}
1623

code_share/app_code_share/templates/app_code_share/code_view.html

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,36 @@
99

1010
{% block body %}
1111

12-
<div class="container">
12+
<div class="container">
1313

14-
{% if messages %}
15-
<ul class="messages">
16-
{% for message in messages %}
17-
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
18-
{% endfor %}
19-
</ul>
20-
{% endif %}
21-
22-
<button id="copy_button">Copy</button>
23-
<input type="url" id="link_box" style="float: right;">
24-
25-
<form action="." enctype="multipart/form-data" method="POST">
26-
{% csrf_token %}
27-
28-
{% if code_share.file_name %}
29-
<h4>File Name: {{code_share.file_name}}</h4>
30-
31-
{%endif%}
32-
33-
<textarea id="code_snippet" name="code_snippet">{{code_share.code}}</textarea><br>
34-
<input id="submit_edit" type="submit" value="edit">
35-
<br>
36-
<a href="{% url 'code_share:app_home'%}" >
37-
<div id="create">create new</div>
38-
</a><br>
39-
40-
</form>
14+
<button id="copy_button">Copy</button>
15+
<input type="url" id="link_box" style="float: right;">
16+
17+
<form action="." enctype="multipart/form-data" method="POST">
18+
{% csrf_token %}
19+
20+
{% if code_share.file_name %}
21+
<h4>File Name: {{code_share.file_name}}</h4>
22+
{%endif%}
23+
24+
<textarea id="code_snippet" name="code_snippet">{{code_share.code}}</textarea><br>
25+
<input id="language" name="language" value="{{code_share.language}}" hidden>
26+
<input id="submit_edit" type="submit" value="edit"><br>
27+
<a href="{% url 'code_share:app_home'%}" >
28+
<div id="create">create new</div>
29+
</a><br>
30+
31+
</form>
4132

4233
<blockquote>
4334
<p><em>Share your shitty code here. nothing is necessary. Put code and submit.BAM.</em></p>
4435
</blockquote>
4536

46-
</div>
47-
37+
</div>
4838

4939
{% endblock %}
5040

5141

5242
{% block scripts%}
53-
<script type="text/javascript" crossorigin="anonymous" src="{% static 'js/codeshare.js' %}"></script>
54-
{% endblock %}
43+
<script type="text/javascript" crossorigin="anonymous" src="{% static 'js/codeshare.js' %}"></script>
44+
{% endblock %}

code_share/app_code_share/templates/app_code_share/homepage.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@
1111

1212
<div class="container">
1313

14-
{% if messages %}
15-
<ul class="messages">
16-
{% for message in messages %}
17-
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
18-
{% endfor %}
19-
</ul>
20-
{% endif %}
21-
2214
<form action="." enctype="multipart/form-data" method="POST">
2315
{% csrf_token %}
2416

2517
<input type="text" id="file_field" name="file_name" placeholder="File Name" value="{{code_share.file_name}}">
2618
<textarea id="code_snippet" name="code_snippet" placeholder="Code Snippet">{{code_share.code}}</textarea><br>
19+
<input id="language" name="language" value="None" hidden>
2720
<input id="submit_new" type="submit" value="submit" ><br>
2821

2922
</form>

code_share/app_code_share/tests/test_models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ def setUp(self):
1111
self.chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
1212
self.hash_value = get_random_string(8, self.chars)
1313
self.file_name = 'testfilename'
14+
self.language = 'perl'
1415

1516
CodeShare.objects.create(
1617
code=self.code_share,
1718
hash_value=self.hash_value,
18-
file_name=self.file_name
19+
file_name=self.file_name,
20+
language=self.language
1921
)
2022

2123
def test_string_representation(self):

code_share/app_code_share/tests/test_urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ def setUp(self):
1212
self.chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
1313
self.hash_value = get_random_string(8, self.chars)
1414
self.file_name = 'testfilename'
15+
self.language = 'javascript'
1516

1617
CodeShare.objects.create(
1718
code=self.code_share,
1819
hash_value=self.hash_value,
19-
file_name=self.file_name
20+
file_name=self.file_name,
21+
language=self.language
2022
)
2123

2224
def test_detail_reverse(self):

code_share/app_code_share/tests/test_views.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ def setUp(self):
99
self.code_share = 'testcode'
1010
self.chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
1111
self.hash_value = get_random_string(8, self.chars)
12+
self.random_string = get_random_string(8, self.chars)
1213
self.file_name = 'testfilename'
14+
self.language = 'c-like'
1315

1416
CodeShare.objects.create(
1517
code=self.code_share,
1618
hash_value=self.hash_value,
17-
file_name=self.file_name
19+
file_name=self.file_name,
20+
language=self.language
1821
)
1922

2023
def test_post_list(self):
2124
response = self.client.get('/app/' + self.hash_value + '/')
22-
self.assertEqual(response.status_code, 200)
25+
self.assertEqual(response.status_code, 200)

code_share/app_code_share/views.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
from django.shortcuts import render, redirect, get_object_or_404 as goo404
22
from .models import CodeShare
3-
from django.contrib import messages
43
import random
54
from django.utils.crypto import get_random_string
65

76

8-
9-
107
def home(request):
118
"""
129
Homepage and creation of code snippet
@@ -21,6 +18,7 @@ def home(request):
2118
2219
:param code_snippet: code content from the text box
2320
:param file_name: if file name is specified, it is not None
21+
:param language: type of programming language
2422
2523
:return redirect to view_by_hash method with unique ID has param
2624
@@ -31,20 +29,16 @@ def home(request):
3129

3230
if request.method == 'POST':
3331
code_share = request.POST.get('code_snippet')
34-
file_name = request.POST.get('file_name')
32+
file_name = request.POST.get('file_name')
33+
language = request.POST.get('language')
34+
3535
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
3636
hash_value= get_random_string(8, chars)
3737

38-
file_exist = CodeShare.objects.filter(file_name=file_name).exists()
39-
40-
if file_exist is True and file_name != '':
41-
messages.error(
42-
request, 'An error occured')
43-
return render(request, 'app_code_share/homepage.html', {})
44-
4538
CodeShare.objects.create(code=code_share,
4639
hash_value=hash_value,
47-
file_name=file_name)
40+
file_name=file_name,
41+
language=language)
4842
return redirect('code_share:view_by_hash', hash_id=hash_value)
4943

5044

@@ -63,6 +57,7 @@ def view_by_hash(request, hash_id):
6357
handles updation in the content
6458
6559
:param code_snippet: updated code snippet
60+
:param language: type of programming language
6661
6762
:returns redirects to this view again to render the new results
6863
@@ -74,8 +69,10 @@ def view_by_hash(request, hash_id):
7469

7570
if request.method == 'POST':
7671
code_share = request.POST.get('code_snippet')
72+
language =request.POST.get('language')
7773
code_obj = goo404(CodeShare, hash_value=hash_id)
7874
code_obj.code = code_share
75+
code_obj.language = language
7976
code_obj.save()
8077

8178
return redirect('code_share:view_by_hash', hash_id=hash_id)

0 commit comments

Comments
 (0)