Skip to content

Commit

Permalink
Merge pull request #1 from jmartindf/master
Browse files Browse the repository at this point in the history
Removing site specific setting from the plugin
  • Loading branch information
jachin committed Mar 4, 2016
2 parents 7936893 + 47ce150 commit 5cd2fb4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 80 deletions.
115 changes: 102 additions & 13 deletions Pelican.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import sublime_plugin
import threading
import platform, functools
from datetime import date

VERSION = int(sublime.version())
ST2 = VERSION < 3000
if (ST2):
from lib.moveToPosts import getMoveInfo
else:
from Pelican.lib.moveToPosts import getMoveInfo

pelican_slug_template = {
"md": "Slug: %s\n",
Expand Down Expand Up @@ -44,8 +41,30 @@ def run(self,edit):
class PelicanMovePostToContents(sublime_plugin.TextCommand):

def run(self,edit):
root = get_input_path(window=self.view.window())

openfile = self.view.file_name()
(fullPath,newFile) = getMoveInfo(openfile)
fullPath = os.path.abspath(openfile)
fileName = os.path.basename(fullPath)

today = date.today()
yearName = today.strftime("%Y")
monthName = today.strftime("%m")
datePrefix = today.strftime("%Y%m%d")

# Construct the destination folder: content/posts/YYYY/MM
folder = os.path.join(root,"content","posts",yearName,monthName)

# Check if the destination exists, create it if not
try:
os.makedirs(folder)
except OSError:
if not os.path.isdir(folder):
raise

# File format: YYYYMMDD-name
newFile = os.path.join( folder, "%s-%s" % (datePrefix,fileName) )

thread = PelicanMovePostToContentsThread(
self.view, fullPath, newFile)
thread.start()
Expand Down Expand Up @@ -393,7 +412,14 @@ def on_done_post(self, picked):
'insert', {'characters': "{filename}/%s" % path})

def run(self):
self.results = get_categories_tags_from_meta(self.article_paths, mode=self.mode)
blog_details = get_blog_details(self.view)
if "metadata_url" in blog_details and blog_details["metadata_url"] != "":
blog_name = blog_details["name"]
metadata_url = blog_details["metadata_url"]
self.results = get_categories_tags_from_meta(blog_name, metadata_url, mode=self.mode)
else:
self.results = get_categories_tags(self.article_paths, mode=self.mode)

if self.mode == "post":
self.results_full = self.results
self.results = sorted(list(set(self.results)))
Expand Down Expand Up @@ -604,17 +630,24 @@ def parse_makefile(window):
return None


def get_article_paths(window):
article_paths = []

def get_input_path(window):
# load INPUTDIR
inputdir = None
makefile_params = parse_makefile(window)
if makefile_params and "INPUTDIR_"+sublime.platform() in makefile_params:
inputdir = makefile_params["INPUTDIR_"+sublime.platform()]
return makefile_params["INPUTDIR_"+sublime.platform()]
elif makefile_params and "INPUTDIR" in makefile_params:
inputdir = makefile_params["INPUTDIR"]
return makefile_params["INPUTDIR"]
else:
return ""

def get_article_paths(window):
article_paths = []

# load INPUTDIR
inputdir = search_for_root(window)
if inputdir=="":
return []

# get paths of all articles in INPUTDIR
Expand All @@ -630,7 +663,7 @@ def get_article_paths(window):

return article_paths

def get_categories_tags_from_meta(articles_paths, mode="tag"):
def get_categories_tags_from_meta(name, url, mode="tag"):
results = []
# Download the metadata
import urllib.request,urllib.error,json
Expand All @@ -639,11 +672,11 @@ def get_categories_tags_from_meta(articles_paths, mode="tag"):
cache_path = os.path.join(sublime.packages_path(),"Pelican")
if not os.path.exists(cache_path):
os.mkdir(cache_path)
cache_file = os.path.join(cache_path,"meta-minorthoughts.json")
cache_file = os.path.join(cache_path,"meta-%s.json" % name)


try:
response = urllib.request.urlopen('http://minorthoughts.com/meta.json')
response = urllib.request.urlopen(url)
metajson = response.read().decode("utf-8")
except urllib.error.URLError as e:
pass
Expand Down Expand Up @@ -823,3 +856,59 @@ def normalize_article_metadata_case(template_str, normalize_template_var=True):

new_str_lines.append(new_line)
return new_str_lines

# Get the details of the blog from the config file
# "all_blogs": {
# "myblog":
# {
# "blog_path_windows": "C:\\Users\\MyUserName\\Dropbox\\blogFolder",
# "blog_path_osx": "/Users/Me/Dropbox/blogFolder",
# "metadata_url": "http://myblog.com/meta.json"
# }
# },
#
# Returns a dictionary with three keys:
# - name
# - metadata_url
# - root
#
# If there's nothing configured, it will return an empty dictionary
#
def get_blog_details(view):
current_filename = view.file_name()
current_folder = os.path.dirname(current_filename)
current_blog = {}
allBlogs = load_setting(view, "all_blogs", None)
root = ""
metaURL = ""

if allBlogs is not None:
for blog in allBlogs:
blogSettings = allBlogs[blog]
if "blog_path_%s" % sublime.platform() in blogSettings:
blogRoot = blogSettings["blog_path_%s" % sublime.platform()]
if "blog_path" in blogSettings:
blogRoot = blogSettings["blog_path"]
if blogRoot != "" and os.path.commonprefix([blogRoot,current_folder]) == blogRoot: # The current folder is underneath the listed blog root
root = blogRoot
if "metadata_url" in blogSettings:
metaURL = blogSettings["metadata_url"]
break

if root != "":
current_blog["name"] = blog
current_blog["metadata_url"] = metaURL
current_blog["root"] = root
return current_blog

# Look in multiple places to figure out what our root directory is
# First check the config file for explicitly defined blogs
# Next check for a Makefile with an INPUTDIR
#
def search_for_root(window):
view = window.active_view()
details = get_blog_details(view)
if "root" in details:
return details["root"]
return get_input_path(window)

6 changes: 0 additions & 6 deletions Pelican.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
// ====================
// Force the blog paths
// ====================
"blog_path_Windows": "C:\\Users\\jmartin\\Dropbox\\secondcrack\\minorthoughts",
"blog_path_Darwin": "/Users/jmartin/Dropbox/secondcrack/minorthoughts",

// ===============
// Slug generation
// ===============
Expand Down
Empty file removed lib/__init__.py
Empty file.
61 changes: 0 additions & 61 deletions lib/moveToPosts.py

This file was deleted.

0 comments on commit 5cd2fb4

Please sign in to comment.