Skip to content

Commit

Permalink
first release, add test/example
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Feb 10, 2016
1 parent 9d3d600 commit 58adfa8
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 18 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ File Interceptor for [Electron](electron.atom.io)
```javascript

var app = require('electron').app
var posthtml = require('electron-posthtml')([/* PostHTML Plugins */])

var BrowserWindow = require('electron').BrowserWindow

app.on('ready', function () {
var posthtml = require('electron-posthtml')([/* PostHTML Plugins */])

app.on('ready', () => {
view = new BrowserWindow({ width: 800, height: 600 })

view.loadUrl('file://' + __dirname + '/index.html')
})
```

# Example
[Test](https://github.com/michael-ciniawsky/electron-posthtml//tree/master/test)
29 changes: 15 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var getPath = function (url) {
var parsed = require('url').parse(url)
var result = parsed.pathname

// Local files in windows start with slash if no host is given
// file:///c:/file.html
// Local files in windows start with a slash if no host is given
// file:///C:/file.html
if (process.platform === 'win32' && !parsed.host.trim()) {
result = result.substr(1)
}
Expand All @@ -23,26 +23,27 @@ var getPath = function (url) {
}

module.exports = function (plugins) {
plugins = plugins || []

app.on('ready', function () {
var protocol = require('electron').protocol
var plugins = plugins || []
const protocol = require('electron').protocol

protocol.interceptBufferProtocol('file', (request, callback) => {
protocol.interceptBufferProtocol('file', function (request, callback) {
var file = getPath(request.url)
var content = null
var source = null

// See if file actually exists
// Check if file exists
try {
content = fs.readFileSync(file)
source = fs.readFileSync(file)

var ext = path.extname(file)

if (ext === '.html') {
var compiled = posthtml(plugins).process(file).then(result => result.html)

return callback({data: new Buffer(compiled), mimeType:'text/html'})
posthtml(plugins)
.process(source)
.then(result => callback({data: new Buffer(result.html), mimeType: 'text/html'}))
} else {
return callback({data: content, mimeType: mime.lookup(ext)})
return callback({data: source, mimeType: mime.lookup(ext)})
}
} catch (e) {
// See here for error numbers:
Expand All @@ -58,9 +59,9 @@ module.exports = function (plugins) {
}
}, (error, scheme) => {
if (!error) {
console.log('PostHTML interceptor success')
console.log('PostHTML loaded')
} else {
console.error('PostHTML interceptor error:', error)
console.error('PostHTML error:', error)
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-posthtml",
"version": "0.0.3",
"version": "0.8.0",
"description": "PostHTML file interceptor for Electron",
"main": "index.js",
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions test/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions test/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
11 changes: 11 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# OS

._*
.DS_Store

# NodeJS

npm-debug.log

node_modules
bower_components
21 changes: 21 additions & 0 deletions test/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Michael Ciniawsky <michael.ciniawsky@gmail.com> (http://michael-ciniawsky.de)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
23 changes: 23 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# PostHTML Electron
## Install

```
$ sudo npm i
```

## Start

```
$ npm start
```

## Build

```
$ npm run build
```

Builds the app for OS X, Linux, and Windows, using [electron-packager](https://github.com/maxogden/electron-packager).

## License
MIT © [Michael Ciniawsky](http://michael-ciniawsky.de)
Binary file added test/client/gir.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/client/include/section.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div block="component">
<h1>Component</h1>
<div elem="section">Section</div>
<div elem="section" mods="modified">Section modified</div>
</div>
28 changes: 28 additions & 0 deletions test/client/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
header {
display: flex;
align-items: center;
justify-content: center

width: 500px;
height: 250px;

color: white
text-align: center;
}

header h1 {
font-size: 2rem;
font-weight: 100;
font-family: 'Helvetica Neue', Helvetica, sans-serif;
}

main {
display: flex;
align-items: center;
justify-content: center
}

main img {
width: 200px;
height: 200px;
}
24 changes: 24 additions & 0 deletions test/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Electron</title>
<link rel="stylesheet" href="index.css">
</head>

<body>
<div class="container">
<header block="header">
<h1 elem="title">Gir's Electron Boilerplate</h1>
</header>
<main block="main">
<img elem="img" src="gir.jpg" alt="Gir">
<include src="include/section.html"></include>
</main>
</div>

<script src="index.js"></script>
</body>

</html>
30 changes: 30 additions & 0 deletions test/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 'use strict'
//
// const remote = require('electron').remote
// const Menu = remote.Menu
// const MenuItem = remote.MenuItem
//
// let menu = new Menu()
//
// menu.append(new MenuItem({
// label: 'Electron'
// }))
// menu.append(new MenuItem({
// type: 'separator'
// }))
// menu.append(new MenuItem({
// label: 'Menu', type: 'checkbox', checked: true
// }))
//
// window.addEventListener('contextmenu', (e) => {
// e.preventDefault()
// menu.popup(remote.getCurrentWindow())
// }, false)
//
// let notification = new Notification('Title', {
// body: 'Lorem Ipsum Dolor Sit Amet'
// })
//
// notification.onclick = () => {
// console.log('Notification clicked')
// }
34 changes: 34 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

var app = require('electron').app

var bem = require('posthtml-bem')
var include = require('posthtml-include')

var posthtml = require('electron-posthtml')([include({root: './client/'}), bem()])

const BrowserWindow = require('electron').BrowserWindow

// require('electron').crashReporter.start()

app.on('ready', () => {
// Main Window
let main = new BrowserWindow({
width: 1200,
height: 900,
position: 'center',
resizable: true,
frame: false
})
main.loadURL(`file://${__dirname}/client/index.html`)
main.webContents.openDevTools()
main.on('closed', () => {
main = null
})
})

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
37 changes: 37 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "electron",
"version": "0.36.7",
"description": "Electron Boilerplate",
"license": "MIT",
"main": "index.js",
"files": [
"client/index.js",
"client/index.html",
"client/index.css"
],
"engines": {
"node": ">=4"
},
"scripts": {
"start": "electron .",
"build": "electron-packager . $npm_package_productName --out=dist --ignore=dist --prune --asar --all --version=0.36.3"
},
"dependencies": {
"electron-posthtml": "^0.8.0",
"posthtml-bem": "^0.2.2",
"posthtml-include": "^1.0.2"
},
"devDependencies": {
"electron-debug": "^0.5.1",
"electron-packager": "^5.2.0",
"electron-prebuilt": "^0.36.7"
},
"author": {
"name": "Michael Ciniawsky",
"email": "michael.ciniawsky@gmail.com",
"url": "michael-ciniawsky.de"
},
"keywords": [
"Electron"
]
}

0 comments on commit 58adfa8

Please sign in to comment.