I want to build the haml in the sublime text 2 without switch to the iterm. So I build a simple plugin for the sublime, just like this.
import os
import sublime, sublime_plugin
class HamlToHtmlCommand(sublime_plugin.TextCommand):
def run(self,edit):
source = self.view.file_name()
filefullname = source.split('/')[-1]
filename = filefullname.split('.')[0]
target = "/".join(source.split('/')[0:-1])
com = "haml " + source + " > " + target + "/" + filename +'.html'
os.system(com)
def is_enabled(self):
return True
But the problem is that, when i build in the sublime, the target html file is empty.
for example, the "com" is "haml /Users/latpaw/login_register/login.haml /Users/latpaw/login_register/login.html". But if do the os.system(com) in the python cli, it is right.
So what really happens here
By the way, I really don't know Haml, so at first I just tried to fix your code. But I think you can build Haml directly from ST, by going to Tools→Build (⌥B). Or create a new build system Tools→Build Systems→New build system. Apparently you have the following variables that you could use:
$file The full path to the current file, e. g., C:\Files\Chapter1.txt.
$file_path The directory of the current file, e. g., C:\Files.
$file_name The name portion of the current file, e. g., Chapter1.txt.
$file_extension The extension portion of the current file, e. g., txt.
$file_base_name The name only portion of the current file, e. g., Document.
$packages The full path to the Packages folder.
$project The full path to the current project file.
$project_path The directory of the current project file.
$project_name The name portion of the current project file.
$project_extension The extension portion of the current project file.
$project_base_name The name only portion of the current project file.
Apparently you have an issue with path escaping. You must have, like me when I tried, a space somewhere in the path of your file. Or you probably know that commands like cat ~/Sublime Text/test do not work well. You can either:
cat ~/Sublime\ Text/test
cat ~/'Sublime Text/test'
I would advise the later and do this:
com = "haml '" + source + "' > '" + target + "/" + filename +"'.html"
PS: arguably, a better solution would be to add these single quotes directly when defining source, target and filename.
Related
I have about 100 input files which, after processing, generate more than 2000 output files. I would like to name the output files based on the names of the input file.
Here is the command I run:
Start cmd /k "G:Path\eachGeo.bat G:\Path\InputGeo\*.csv"
The input files are read via cmd by executing the .bat file. Output is stored at a different path:
outputfilename = 'Path\outputGeo\\' + Time.now.to_i.to_s +
'_' + eachTag[45..54] + '_output.csv'
In the code above I am using Time.now.to_i.to_s to name the output files based on the current system time.
I would like to change this to be the name of the input file.
Normally you'd tackle it like this where you're using things like File.basename to extract the relevant part of the original file path:
Dir.glob("path/*.csv") do |path|
CSV.open(path) do |csv_in|
# ...
out_path = "output_path/%s_%s.csv" % [
File.basename(path, ".csv"),
each_tag[45..54]
]
CSV.open(out_path, "w") do |csv_out|
# ...
end
end
end
This is a really simple example. I'd avoid putting your output files in the same directory as the input ones so you don't mistakenly read them in again when you run the program a second time.
trying to setup my gulp file for a project, I want to compile the less, and then minify it and have it save this to a new folder.
My less file is saved in app/less/styles.less, the compiled css file should save to dist/css/styles.css but it's saving to dist/css/less/styles.css.
What am I doing wrong here?
var app = 'app/',
dist = 'dist/',
appStyles = app + '**/*.less';
gulp.task('compilecssremote', function(){
return gulp.src(appStyles)
.pipe(plumber({
errorHandler: onError
}))
.pipe(changed(dist)) //must be dist
.pipe(urladjuster({
prepend: '/' + project + '/dist/' //based on location of CSS files
}))
.pipe(less())
.pipe(minifycss({keepBreaks: false}))
.pipe(gulp.dest(dist + 'css'))
});
This is the expected behavior when you use a gulp.src like 'app/**/*.less (aka your appStyles) to match source files with paths like app/less/styles.less. There are two pieces to understand here:
By default everything before the first glob in your gulp.src path will be omitted from the output path. In this case that's everything before the **, which is to say the app/. That's why the your output css file isn't going to 'dest/app/…. (For a more detailed discussion of this, see this answer)
The path matched by the first glob and on is preserved in the output path. In this case, that's the less/ in the matched file 'app/less/styles.less' (that is, the part of 'app/**/*.less' represented by **). (It's beside the point in the case of your problem, but this feature is very useful for preserving relative file structures.)
Using #1 & #2, one quick fix would be to trim less/ from the output path by putting it before the first glob in the gulp.src. For example, change appStyles = app + 'less/**/*.less'.
I have a variable with file path like:
$file = '/some/file.txt';
or
var file = '/some/file.txt'
To edit file.txt I:
Left click on string.
Do Extend Selection shortcut.
Do Navigate File... shortcut.
Can I do it faster? For example, by clicking on '/some/file.txt' with some modifier key.
Install and use Navigate From Literal plugin -- it works with any strings as it matches files by names.
It's not 100% perfect though .. as it seems to work rather with file names and even though the path in string is pretty unique .. it most likely will show a choice for all files named the same (e.g. file.txt in your case) -- at least this is what I remember when using it.
I can press Ctrl + Shift + F in PhpStorm, and I will get a window with search options. I can search for particular string in all project files, I can leave "Text to find" field empty and put name of the file I'm looking for in "File mask(s)" field to find a particular file, but is there a way to find a folder by its name?
In Find in Path dialog (Ctrl + Shift + F) -- No.
But you can use Navigate | File... for that (Ctrl + Shift + N on Windows/Linux using Default keymap)
The key is to use path separator / to denote that the previous part was a folder.
Same as actual search for file name: the directory name can be partial: phpd/ will match phpdoc/ if that the only folder. Similar with capital letters: mff/ will match MyFavouriteFolder/.
File or Folder right click > Mark Directory as > Excluded
Only blue directories will be searched
What would be the best method, please, to insert file (foo.txt) into open file (bar.txt) at caret position?
It would be nice to have an open-file dialog to choose anything to be inserted.
The word processing equivalent would be "insert file" here.
Here is a substitute for foo.sublime-snippet, which can be linked to form files elsewhere:
import sublime, sublime_plugin
class InsertFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
template = open('foo.txt').read()
print template
v.run_command("insert_snippet", {"contents": template})
From within a text command you can access the current view. You can get the cursor positions using self.view.sel(). I don't know how to do gui stuff in python, but you can do file selection using the quick panel (similar to FuzzyFileNav).
Here is my unofficial modification of https://github.com/mneuhaus/SublimeFileTemplates which permits me to insert-a-file-here using the quick panel. It works on an OSX operating system (running Mountain Lion).
The only disadvantage I see so far is the inability to translate a double-slash \\ in the form file correctly -- it gets inserted instead as just a single-slash \. In my LaTex form files, the double-slash \\ represents a line ending, or a new line if preceded by a ~. The workaround is to insert an extra slash at each occurrence in the actual form file (i.e., put three slashes, with the understanding that only two slashes will be inserted when running the plugin). The form files need to be LF endings and I'm using UTF-8 encoding -- CR endings are not translated properly. With a slight modification, it is also possible to have multiple form file directories and/or file types.
import sublime, sublime_plugin
import os
class InsertFileCommand(sublime_plugin.WindowCommand):
def run(self):
self.find_templates()
self.window.show_quick_panel(self.templates, self.template_selected)
def find_templates(self):
self.templates = []
self.template_paths = []
for root, dirnames, filenames in os.walk('/path_to_forms_directory'):
for filename in filenames:
if filename.endswith(".tex"): # extension of form files
self.template_paths.append(os.path.join(root, filename))
self.templates.append(os.path.basename(root) + ": " + os.path.splitext(filename)[0])
def template_selected(self, selected_index):
if selected_index != -1:
self.template_path = self.template_paths[selected_index]
print "\n" * 25
print "----------------------------------------------------------------------------------------\n"
print ("Inserting File: " + self.template_path + "\n")
print "----------------------------------------------------------------------------------------\n"
template = open(self.template_path).read()
print template
view = self.window.run_command("insert_snippet", {'contents': template})
sublime.status_message("Inserted File: %s" % self.template_path)