closeheat-editor
Version:
Edit deployed websites.
50 lines (35 loc) • 1.76 kB
text/coffeescript
Filesystem = require('./filesystem')
_ = require 'lodash'
class TextReplacer
constructor: (, , ) ->
replace: ->
return if _.isEmpty(.content_position)
(, , .content_position.start,
.content_position.end)
replaceAtCoords: (string, insertion, start, end) ->
string.substr(0, start) + insertion + string.substr(end)
class AttributeReplacer
constructor: (, , ) ->
getAtCoords: (string, start, end) ->
string.substr(start, end - start)
regex: (attribute) ->
new RegExp(new RegExp("(\\s*(?:\\s+#{attribute.name})\\s*)").source + /(=\s*")([^"]*)("\s*)/.source)
replaceAttribute: (string, attribute) ->
string.replace((attribute), "$1$2#{attribute.value}$4")
replace: ->
tag = (, .start_tag_position.start, .start_tag_position.end)
_.each , (attribute) =>
tag = (tag, attribute)
(, tag, .start_tag_position.start,
.start_tag_position.end)
replaceAtCoords: (string, insertion, start, end) ->
string.substr(0, start) + insertion + string.substr(end)
module.exports =
class SourceModifier
constructor: (, , ) ->
= Filesystem.read(.file_path).content
apply: ->
replaced_text = new TextReplacer(, , ).replace()
replaced_attributes = new AttributeReplacer(, , replaced_text).replace()
Filesystem.write(.file_path, replaced_attributes)
console.log replaced_attributes