Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add charset parmater #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"crypto": "~0.0.3",
"express": "^4.16.4",
"iconv-lite": "^0.5.1",
"lodash": "^4.17.11",
"lorem-ipsum": "~0.1.1",
"moment": "^2.24.0",
Expand Down
25 changes: 22 additions & 3 deletions web.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ morgan = require 'morgan'
loremIpsum = require 'lorem-ipsum'
seedRandom = require 'seed-random'
crypto = require 'crypto'
iconv = require 'iconv-lite'

app = express()

Expand Down Expand Up @@ -122,6 +123,9 @@ app.get '/', (request, response) ->
for month it must evenly divide 12, and for day and year it
can only be 1.
</li>
<li>
<em>charset</em>: xml charset <a href="https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings">support charset</a>
fengkx marked this conversation as resolved.
Show resolved Hide resolved
</li>
</ul>
<h2>Examples</h2>
<ul class="disc indent">
Expand All @@ -143,6 +147,9 @@ app.get '/', (request, response) ->
<li>
Update once a year: <a href="/feed?unit=year">/feed?unit=year</a>
</li>
<li>
Output as gbk encoding: <a href="/feed?charset=gbk">/feed?charset=gbk</a>
</li>
<li>
<strong>Invalid example:</strong>
update every 7 minutes (does not evenly divide 60):
Expand Down Expand Up @@ -182,7 +189,7 @@ app.get '/feed', (request, response) ->
pubDate = getNearest(interval, unit)

feed = new RSS({
title: "Lorem ipsum feed for an interval of #{interval} #{unit}s",
title: "Lorem ipsum feed change every #{interval} #{unit}s" + ' 你好世界(charset test)',
fengkx marked this conversation as resolved.
Show resolved Hide resolved
description: 'This is a constantly updating lorem ipsum feed'
site_url: 'http://example.com/',
copyright: 'Michael Bertolacci, licensed under a Creative Commons Attribution 3.0 Unported License.',
Expand All @@ -206,9 +213,21 @@ app.get '/feed', (request, response) ->

etagString = feed.pubDate + interval + unit

response.set 'Content-Type', 'application/rss+xml'
response.set 'ETag', "\"#{crypto.createHash('md5').update(etagString).digest("hex");}\""
response.send feed.xml()
console.log(request.query)
fengkx marked this conversation as resolved.
Show resolved Hide resolved
if request.query.charset?
charset = request.query.charset
else
charset = 'utf8'
if charset == 'utf8'
response.set 'Content-Type', 'application/rss+xml'
xmlText = feed.xml()
else
response.set 'Content-Type', 'application/rss+xml;'+'charset=' + charset + ';'
xmlText = feed.xml()
xmlText = xmlText.replace /encoding="([\w-]+?)"/, 'encoding="' + charset + '"';
xmlText = iconv.encode(xmlText, charset);
response.send xmlText


port = process.env.PORT || 5000;
Expand Down
115 changes: 107 additions & 8 deletions web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.