<commit_before>from datetime import datetime
import functools
import os
import re
import shutil
import sys
import traceback

import sublime
import sublime_plugin

PY3 = sys.version > '3'
ST3 = sublime.version() >= '3000'


## Exception Decorator                                                       ##
## ------------------------------------------------------------------------- ##
# This function allows for custom exceptions while preserving the stacktrace. #
# See discussion on Stack Overflow here: http://stackoverflow.com/a/9006442   #
## ------------------------------------------------------------------------- ##

class MissingPathException(Exception):
    pass


def catch_errors(fn):
    @functools.wraps(fn)
    def _fn(*args, **kwargs):
        try:
            return fn(*args, **kwargs)

        except MissingPathException:
            sublime.error_message('Jekyll: Unable to find path information in Jekyll.sublime-settings.')
            user_settings_path = os.path.join(sublime.packages_path(), 'User', 'Jekyll.sublime-settings')
            if not os.path.exists(user_settings_path):
                default_settings_path = os.path.join(sublime.packages_path(), 'Jekyll', 'Jekyll.sublime-settings')
                shutil.copy(default_settings_path, user_settings_path)
            sublime.active_window().open_file(user_settings_path)

        except:
            traceback.print_exc()
            sublime.error_message('Jekyll: unknown error (please, report a bug!)')

    return _fn


def get_setting(view, key, default=None):
    """
    Get a Sublime Text setting value, starting in the project-specific
    settings file, then the user-specific settings file, and finally
    the package-specific settings file. Also accepts an optional default.

    """
    try:
        settings = view.settings()
        if settings.has('Jekyll'):
            s = settings.get('Jekyll').get(key)
            if s and len(s) > 0:
                return s
            else:
                pass
        else:
            pass
    except:
        pass
    global_settings = sublime.load_settings('Jekyll.sublime-settings')
    return global_settings.get(key, default)


def get_syntax_path(view, syntax='Markdown'):
    """
    Sets the view syntax.

    """
    full_syntax = os.path.join('Jekyll', 'Syntaxes', '{0} (Jekyll).tmLanguage'.format(syntax))

    if PY3:
        syntax_path = os.path.join('Packages', full_syntax)

        if sublime.platform() == 'windows':
            syntax_path = full_syntax.replace('\\', '/')
        try:
            sublime.load_resource(syntax_path)
            view.set_syntax_file(syntax_path)
        except:
            pass
    else:
        syntax_path = os.path.join(sublime.packages_path(), full_syntax)
        if os.path.exists(syntax_path):
            view.set_syntax_file(syntax_path)


class JekyllNewPostBase(sublime_plugin.WindowCommand):
    """
    A Sublime window command base class for creating Jekyll posts.

    """
    def doCommand(self):
        post_type = 'draft' if self.IS_DRAFT else 'post'
        self.window.show_input_panel(
            'Jekyll {0} title:'.format(post_type),
            '',
            self.title_input,
            None,
            None
        )

    @catch_errors
    def posts_path_string(self):
        p = get_setting(self.window.active_view(), 'posts_path')
        if not p or p == '':
            raise MissingPathException
        return p

    @catch_errors
    def drafts_path_string(self):
        p = get_setting(self.window.active_view(), 'drafts_path')
        if not p or p == '':
            raise MissingPathException<commit_msg>plugin searches the view's folders for a jekyll blog if default blog isn't set<commit_after>from datetime import datetime
import functools
import os
import os.path
import re
import shutil
import sys
import traceback

import sublime
import sublime_plugin

PY3 = sys.version > '3'
ST3 = sublime.version() >= '3000'


## Exception Decorator                                                       ##
## ------------------------------------------------------------------------- ##
# This function allows for custom exceptions while preserving the stacktrace. #
# See discussion on Stack Overflow here: http://stackoverflow.com/a/9006442   #
## ------------------------------------------------------------------------- ##

class MissingPathException(Exception):
    pass


def catch_errors(fn):
    @functools.wraps(fn)
    def _fn(*args, **kwargs):
        try:
            return fn(*args, **kwargs)

        except MissingPathException:
            sublime.error_message('Jekyll: Unable to find path information in Jekyll.sublime-settings.')
            user_settings_path = os.path.join(sublime.packages_path(), 'User', 'Jekyll.sublime-settings')
            if not os.path.exists(user_settings_path):
                default_settings_path = os.path.join(sublime.packages_path(), 'Jekyll', 'Jekyll.sublime-settings')
                shutil.copy(default_settings_path, user_settings_path)
            sublime.active_window().open_file(user_settings_path)

        except:
            traceback.print_exc()
            sublime.error_message('Jekyll: unknown error (please, report a bug!)')

    return _fn


def get_setting(view, key, default=None):
    """
    Get a Sublime Text setting value, starting in the project-specific
    settings file, then the user-specific settings file, and finally
    the package-specific settings file. Also accepts an optional default.

    """
    try:
        settings = view.settings()
        if settings.has('Jekyll'):
            s = settings.get('Jekyll').get(key)
            if s and len(s) > 0:
                return s
            else:
                pass
        else:
            pass
    except:
        pass
    global_settings = sublime.load_settings('Jekyll.sublime-settings')
    return global_settings.get(key, default)


def get_syntax_path(view, syntax='Markdown'):
    """
    Sets the view syntax.

    """
    full_syntax = os.path.join('Jekyll', 'Syntaxes', '{0} (Jekyll).tmLanguage'.format(syntax))

    if PY3:
        syntax_path = os.path.join('Packages', full_syntax)

        if sublime.platform() == 'windows':
            syntax_path = full_syntax.replace('\\', '/')
        try:
            sublime.load_resource(syntax_path)
            view.set_syntax_file(syntax_path)
        except:
            pass
    else:
        syntax_path = os.path.join(sublime.packages_path(), full_syntax)
        if os.path.exists(syntax_path):
            view.set_syntax_file(syntax_path)

def get_local_path(window, folder_name):
    for folder in window.folders():
        local_folder = os.path.join(folder, folder_name)
        if os.path.exists(local_folder):
            return local_folder
        return None


class JekyllNewPostBase(sublime_plugin.WindowCommand):
    """
    A Sublime window command base class for creating Jekyll posts.

    """
    def doCommand(self):
        post_type = 'draft' if self.IS_DRAFT else 'post'
        self.window.show_input_panel(
            'Jekyll {0} title:'.format(post_type),
            '',
            self.title_input,
            None,
            None
        )

    @catch_errors
    def posts_path_string(self):
        p = get_setting(self.window.active_view(), 'posts_path')
        if not p or p == '':
            backup = get_local_path(self.window, '_posts')
            if not backup:
                raise MissingPathException
            return backup
        return p

    @catch_errors
    def drafts_path_string(self):
        p = get_setting(self.window.active_view(), 'drafts_path')
        if not p or p == '':
            backup = get_local_path(self.window, '_drafts')
            if not backup:
                raise MissingPathException
            return backup
{'fraction_non_alphanumeric': 0.13409815168897388, 'fraction_numerical': 0.004588910133843212, 'mean_word_length': 2.0505443234836704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5720348', 'n_tokens_mistral': 2292, 'n_tokens_neox': 2187, 'n_words': 524}